In this Python post, we’ll learn what Python list count is and how to properly use it in Python code. We’ll be going through multiple examples to practically demonstrate how the count method of Python list works.
After reading this article, I am sure you’ll be able to easily incorporate Python list count method in your own code.
So let’s not waste anymore time and dive right into its practical implementation.
What is Python List Count?
The count method of Python list is used to specify how many times a specific item is shown in the list. We’ll have to pass an item to the count method to see how many times it’s used in that list.
Let’s now understand the usage of Python list count method using practical examples. But first, let’s understand its syntax.
Syntax of Count Method
listName.count(item)
You can see that the count method accepts a single argument. We’ve to pass a specific value to it so this method can count the number of times this value appears in the list.
Implementing Python List Count Method (Multiple Easy Examples)
Below examples practically demonstrates the usage and purpose of count method.
Example 1: Counting an Item in List
In this example, we’ll first create a list and pass it some items. Then we’ll pass a specific item to the count method to get the number of times its appears in that list. See below code:
listofItems= [1,2,3,2,4,5,22,6,7,2,8,9,10,2] print(listofItems.count(2)) // 4
You can see that we have passed the value 2 to the count method and the method then checked that specific value inside list. It appeared 4 times in that list so the count method returned 4.
If you want to count the number of times a string appeared in list then use the below code:
listofItems= [1,2,3,'green',2,4,5,'green',22,6,7,2,8,9,'green',10,2] print(listofItems.count('green')) // 3
You can use the same process for float(decimal value) as well.
Example 2: Counting Specific Nested Lists
We will now see how we can count the number of nested lists using count method. See below code:
listofItems= [1,2,3,[2,4],3,5,10,[2,4],2,3.2,'white',[2,4]] print( listofItems.count( [2,4] )) // 3
This is how you can easily count the number of nested lists as well. You just have to pass that specific list to the count method as shown in the above code.
Example 3: Counting Specific Tuples in List
listofItems= [1,2,3,3,5,(2,4),10,2,3.2,(2,4),'white'] print(listofItems.count((2,4))) //2
You can count the number of specific tuples by following this code. Use the same method to count the number of specific sets inside list.
Example 4: Returns zero if Specified Item is not Found
The Python list count method returns 0 if the specified item is not present in the list. See below example:
listofItems= [1,2,3,3,5] print(listofItems.count(24)) // 0
So this is how you can easily implement Python list count method. Hope your queries are now cleared regarding the implementation of Python list count.
Feel free to ask if you still have any questions related to the count method of Python list. I’d be really happy to answer all.
Conclusion
To conclude this post, hope you now have a detailed practical understanding of how to use Python list count method. Your feedback would be well appreciated. Thank you for reading this post.