Instruction
Good job! Let's move on to count(), which counts the number of occurrences of a given element in a structure. How many times did we process 18 requests in one day? Let's find out:
requests_processed = [12, 18, 15, 23, 24, 8, 0, 18, 14, 13] requests_processed.count(18)
The code above returns 2 because the number 18 appears twice in the list. Note that the syntax is a bit different than before: We use structure_name.count(element) to find the answer.
Internally, count() iterates over the list and keeps a temporary variable with the current count of the given element. After the iteration process, that temporary value is returned. You could easily write such a function yourself, but using count() makes your code shorter and easier to read. Other developers will also be able to quickly understand your code.
Exercise
Write a function named add_unique(initial_list, new_elements_list) that accepts two arguments:
initial_list– a list with any number of elements.new_elements_list– a list with new elements that should be added toinitial_list.
However, any element from new_elements_list that already occurs more than once in initial_list should NOT be added. Also, two identical elements should appear in the list at most twice.
For example, for the following input:
initial_list = [1, 5, 5, 3] new_elements_list = [4, 5, 6, 3, 3]
The function should return:
initial_list = [1, 5, 5, 3, 4, 6, 3]
You can use the template code to test your solution.
Stuck? Here's a hint!
To add an element to a list, use:
list_name.append(element)



