Python - Count elements in list
You can use the len()
function to count the number of elements in a list. Here's an example:
This will output 5
, which is the number of elements in the list.
You could also use collections.Counter()
for counting the items in list
from collections import Counter
my_list = [1,2,3,4,5,5,5,5,5,5]
count = Counter(my_list)
print(count)
this will output Counter({5: 5, 1: 1, 2: 1, 3: 1, 4: 1})
which contains all unique elements and count of their occurence in the list.