Skip to content

Python List count() Method Malli Spark By {Examples}

  • by

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 lis. The count() method of a list in Python 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 how we can 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: Use list.count() method
mylist = [2, 1, 4, 3, 2, 3, 2, 3, 4, 5, 3, 7]
count1 = mylist.count(3)
count2 = mylist.count(9)

# Example 4: Count multiple items
mylist = [“Python”, “Spark”, “Python”, “Hadoop”, “Python”, “Pandas”]
count = Counter(mylist)

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

# Example 6: 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 7: 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)]

# 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 return 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 object “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 counts 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.

You can also count() method takes one argument, which is the element you want to count in the list. It then returns the number of times that element appears in the list. For example, mylist.count(3) returns the count of how many times an element 3 occurs in the list mylist, which is 4. The count is then assigned to the variable count and printed to the console.

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

# Use list.count() method
count = mylist.count(3)
print(‘Count the number of elements:’, count)

Yields below output.

You can also use the count() method of a list in Python returns the number of times a specified element appears in the list. If the element is not present in the list, it returns 0. For example, count1 is the number of times the element 3 appears in mylist, which is 4. 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
count1 = mylist.count(3)
count2 = mylist.count(9)
print(‘Count the number of elements:’, count1)
print(‘Count the number of elements:’, count2)

# Output:
# Count the number of elements: 4
# Count the number of elements: 0

4. Count Multiple Items

You can also use count multiple items in a list, you can use the Counter() method from the collections module. For example, you first import the Counter class from the collections module. you then create a list mylist containing multiple items. You can pass mylist as an argument to the Counter() method to create a dictionary containing the count of each item in the list. You store this dictionary in a variable count and then 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.

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.

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 tuples and lists. 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. The method returns the count, which is then stored in the variables count_of_tuple and count_of_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.

Conclusion

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

Happy Learning !!

 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 lis. The count() method of a list in Python returns the number of times a specified element appears in the list. If the element  Read More Python, Python Tutorial, Python List Methods 

Leave a Reply

Your email address will not be published. Required fields are marked *