Python List count() Method

How to count a list in Python? The count() method in Python is a built-in function that can be used to count the number of occurrences of an element in a list. This returns the number of times a specified element appears in the list. If the element is not present in the list, it returns zero.

In this article, I will explain the Python list count() method syntax, parameters, and usage of how to count the number of occurrences of elements from a given list with examples.

1. Quick Examples of List count() Method

If you are in a hurry, below are some quick examples of the list count() method.


# Quick examples of list count() method

# Example 1: Use list.count() method
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Pandas"]
count = mylist.count("Python")

# Example 2: Use list.count() method
mylist = [2, 1, 4, 3, 2, 3, 2, 3, 4, 5, 3, 7]
count = mylist.count(3)

# Example 3: Count multiple items
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Pandas"]
count = Counter(mylist)

# Example 4: Use raise a TypeError
mylist = [2, 1, 4, 3, 2, 3, 2, 3, 4, 5, 3, 7]
count = mylist.count()

# Example 5: Count the number of times the tuple 
mylist = [(2, 4), [1, 3], (2, 4), [5, 6], [1, 3]]
count_of_tuple = mylist.count((2, 4))
# Count the number of times the list
count_of_list = mylist.count([1, 3])

# Example 6: Get the number of occurrences
# of each element in a list
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Spark"]
count = [ [l, mylist.count(l)] for l in set(mylist)]

# Example 7: Get the number of occurrences
# of each element in a dictionary
count = dict( (l, mylist.count(l) ) for l in set(mylist))

2. Python List count() Method

You can use the count() method of a Python list to get the number of times an element is present in a list. It is a useful method for counting the occurrences of a particular element within a list.

2.1 Syntax of count()

Following is the syntax of the list count() method.


# Syntax of count() method
list.count(element)

2.1 Parameter of count()

  • element – This parameter is required, for the element for which you want to count the occurrences in the list.

2.2 Returns Value

The count() method in Python returns the number of times a specified element appears in a list.

3. Python count() Example

You can use the count() method of a Python list to return the count of how many times a specified object occurs in the list. For example, mylist.count("Python") returns the count of how many times the value "Python" appears in the list mylist, which is 3. The count is then assigned to the variable count and printed to the console.

Note that the count() method only count the occurrences of an element in the list. It does not modify the list in any way.


# Initialize list
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Pandas"]
print("Original list: ", mylist)

# Use list.count() method
count = mylist.count("Python")
print('Count the number of lists of python:', count)

Yields below output.

python list count

If the element is not present in the list, it returns 0. For example, count2 is the number of times the element 9 appears in mylist, which is 0 because 9 is not present on the list.


# Initialize list
mylist = [2, 1, 4, 3, 2, 3, 2, 3, 4, 5, 3, 7]
print("Original list: ", mylist)

# Use list.count() method
count2 = mylist.count(9)
print('Count the number of elements:', count2)

# Output:
# Count the number of elements: 0

4. Count Multiple Items

You can also use count multiple items in a list by using the Counter() method from the collections module.

In the below example, first import the Counter class from the collections module, then create a list mylist containing multiple items. Pass mylist as an argument to the Counter() method to create a dictionary containing the count of each item in the list. Store this dictionary in a variable count and finally print it out using the print() function.


from collections import Counter

# Count multiple items
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Pandas"]
count = Counter(mylist)
print(count)

Yields below output.

python list count

5. Use TypeError

You can also use the count() method in Python requires a parameter to be passed, which is the value that you want to count in the list. If you don’t pass a parameter to the count() method, it will raise a TypeError with a message that says "count() takes exactly one argument (0 given)".


# Initialize list
mylist = [2, 1, 4, 3, 2, 3, 2, 3, 4, 5, 3, 7]
print("Original list: ", mylist)

# This will raise a TypeError
count = mylist.count()
print('Count the number of elements:', count)

Yields below output.

python list count

6. Count Tuple and List Elements Inside List

You can also use the count() method to count the number of occurrences of tuples and lists inside a list as well. The count() method works with any object that can be found in the list, including tuples, lists, strings, and other objects.

In the below example, you have a list that contains combination of tuples and lists as elements. You can use the count() method to count the number of times the tuple (2, 4) and the list [1, 3] appear in the list.


# Initialize list
mylist = [(2, 4), [1, 3], (2, 4), [5, 6], [1, 3]]

# Count the number of times the tuple 
count_of_tuple = mylist.count((2, 4))

# Count the number of times the list
count_of_list = mylist.count([1, 3])

print('Count the number of times the tuple (2, 4) :', count_of_tuple)
print('Count the number of times the list [1, 3] :', count_of_tuple)

Yields below output.

Follow another way to count the occurrences of each element in a list and store the counts in either a list or a dictionary.


# Initialize list
mylist = ["Python", "Spark", "Python", "Hadoop", "Python", "Spark"]
print("Original list: ", mylist)

# Get the number of occurrences
# of each element in a list
count = [ [l, mylist.count(l)] for l in set(mylist)]
print('Count the number list of occurences:', count)
 
# To get the number of occurrences
# of each element in a dictionary
count = dict( (l, mylist.count(l) ) for l in set(mylist))
print('Count the number dictionary of occurences:', count)

Yields below output.

python list count method

Conclusion

In this article, I have explained the Python list count() method syntax, parameter, and usage of how to count the number of occurrences of elements from a given list with examples.

Happy Learning !!

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply