How do you count the number of values in a list Python?
Python List count()In this tutorial, we will learn about the Python List count() method with the help of examples. Show The count() method returns the number of times the specified element appears in the list. Example# create a list numbers = [2, 3, 5, 2, 11, 2, 7]# check the count of 2
count = numbers.count(2)
print('Count of 2:', count)
# Output: Count of 2: 3Python countThe count() is a built-in function in Python. It will return the total count of a given element in a list. The count() function is used to count elements on a list as well as a string. In this Python tutorial, you will learn:
Below are the three solutions:Fastest is using a for loop and storing it in a Dict. import time from collections import Counter def countElement(a): g = {} for i in a: if i in g: g[i] +=1 else: g[i] =1 return g z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4] #Solution 1 - Faster st = time.monotonic() for i in range(1000000): b = countElement(z) et = time.monotonic() print(b) print('Simple for loop and storing it in dict - Duration: {}'.format(et - st)) #Solution 2 - Fast st = time.monotonic() for i in range(1000000): a = Counter(z) et = time.monotonic() print (a) print('Using collections.Counter - Duration: {}'.format(et - st)) #Solution 3 - Slow st = time.monotonic() for i in range(1000000): g = dict([(i, z.count(i)) for i in set(z)]) et = time.monotonic() print(g) print('Using list comprehension - Duration: {}'.format(et - st))Result #Solution 1 - Faster{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 234: 3, 23: 10, 12: 2, 123: 1, 31: 1, 13: 1, 42: 5, 34: 4, 423: 3} Simple for loop and storing it in dict - Duration: 12.032000000000153 #Solution 2 - FastCounter({23: 10, 4: 6, 2: 5, 42: 5, 1: 4, 3: 4, 34: 4, 234: 3, 423: 3, 5: 2, 12: 2, 123: 1, 31: 1, 13: 1}) Using collections.Counter - Duration: 15.889999999999418 #Solution 3 - Slow{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 34: 4, 423: 3, 234: 3, 42: 5, 12: 2, 13: 1, 23: 10, 123: 1, 31: 1} Using list comprehension - Duration: 33.0 Count elements in a flat listSuppose we have a list i.e. To count the elements in this list, we have different ways. Let’s explore them, Use len() function to get the size of a listPython provides a inbuilt function to get the size of a sequence i.e. Arguments:
It returns the length of object i.e. count of elements in the object. Advertisements Now let’s use this len() function to get the size of a list i.e. Output: Number of elements in list : 9 How does len() function works ? When len(s) function is called, it internally calls the __len__() function of the passed object s. Default sequential containers like list, tuple & string has implementation of __len__() function, that returns the count of elements in that sequence. So, in our case we passed the list object to the len() function. Which internally called the __len__() of the list object, to fetch the count of elements in list. Use list.__len__() to count elements in a listWe can directly call the __len__() member function of list to get the size of list i.e. Output: Number of elements in list : 9 Although we got the size of list using __len__() function. It is not a recommended way, we should always prefer len() to get the size of list. (1) Count the Number of Elements in a Python List that Contains StringsTo start with a simple example, let’s create a list that contains 5 names: names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] print(names_list)Run the syntax above, and you’ll get the following list: ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob']You can then use the len() function in order to count the number of elements in the list: names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] print(len(names_list))Once you run the code in Python, you’ll get the count of 5. Let’s extend the list by additional 3 names, and then recount the number of elements: names_list = ['Jeff', 'Ben', 'Maria', 'Sophia', 'Rob'] names_list.extend(['Laura','Elizabeth','Justin']) print(len(names_list))You’ll now get the count of 8. |