Python - Count elements in list
You can use the len() function to count the number of elements in a list.
You can use the len() function to count the number of elements in a list. Here's an example:
Count the number of elements in a list in Python
my_list = [1, 2, 3, 4, 5]
count = len(my_list)
print(count)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
Counting using Counter method in Python
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.