Python list count: How to Count Elements in Python List
Counting several elements in a list in Python is a normal operation and beneficial in many modules. For example, count the number of employees, number of unread notifications, messages, etc. Let’s see how to count the number of elements in the list.
Python list count
To count the specified elements how many times it appears on this list in Python, use the list.count() method. The count() function returns the number of elements with the specified value.
Syntax
list.count(element)
Parameters
The count() function takes a single argument called an element, which is the element to be counted.
Return Value
The count() function returns the number of times an element appears in the list.
Example
# SUPERBOWL list data = ["S", "U", "P", "E", "R", "B", "O", "W", "L"] # count element "P" count = data.count("P") # print the count print('The count of P is:', count) # count element "B" count = data.count("B") # print the count print('The count of B is:', count)
Output
The count of P is: 1 The count of B is: 1
The data is a list containing SUPERBOWL LETTERS.
Finding the count of a list of integer elements
Let’s define a list of integers and count the specific integer element using the count() method.
# Integers list ints = [11, 21, 19, 29, 46, 21, 19] # count element 21 count = ints.count(21) # print the count print('The count of 21 is:', count) # count element 19 count = ints.count(19) # print the count print('The count of 19 is:', count)
Output
The count of 21 is: 2 The count of 19 is: 2
You can see that 21 and 19 are repeated 2 times in the list.
Count Tuple and List Elements Inside List
Let’s define a list of single elements and tuple and find its count using the count() method.
# Integers list list_of_tup = [(11, 21), (19, 29), (46), 21, 19] # count element (11, 21) count = list_of_tup.count((11, 21)) # print the count print('The count of (11, 21) is:', count) # count element 21 count = list_of_tup.count(21) # print the count print('The count of 21 is:', count)
Output
The count of (11, 21) is: 1 The count of 21 is: 1
You can see that (11, 21) tuple appears one time and 21 number is appearing one time in the list.
If you pass more than one parameter to the count() function, it will return the TypeError.
# Integers list li = [1, 2, 3] # count element 2 count = li.count(2, 3) # print the count print('The count is:', count)
Output
Traceback (most recent call last): File "/Users/krunal/Desktop/code/pyt/database/app.py", line 5, in <module> count = li.count(2, 3) TypeError: list.count() takes exactly one argument (2 given)
We got: TypeError: list.count() takes exactly one argument (2 given).
Counting number of Unique Elements in a List
To count the number of unique elements without duplicates, we can use the inbuilt function set() with the list.count() function.
data = [19, 21, 11, 21, 19, 46, 29] count = len(data) unique_count = len(set(data)) print("Number of elements in the list: ", count) print("Number of unique elements in the list: ", unique_count)
Output
Number of elements in the list: 7 Number of unique elements in the list: 5
You can see that the list contains 7 total elements and 5 unique elements.
Counting Elements Using a for Loop
To count the number of elements using for loop, create a function that loops through the list using a for loop. We first initialize the count of items to 0 and every time a loop iteration is performed, the count increases by 1.
final2021 = ["Superbowl", "Buccaneers", 31, "Chiefs", 9] def counting(list): count = 0 for el in list: count += 1 return count print("Number of elements in the list: ", counting(final2021))
Output
Number of elements in the list: 5
This approach is not good since it takes lots of time to execute it. So good programming is to avoid the for loop as much as possible.
Conclusion
The count() is an inbuilt Python that returns the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.