Python List Count() Method

When working with a Python list, you may often need to determine how many times a specific item appears. This is where the built-in count() method in Python becomes invaluable. It provides a direct, efficient way to count occurrences in a list in Python without writing custom loops or logic. Whether you’re handling strings, numbers, or mixed data types, the Python count method helps simplify many data processing tasks. In this article, we’ll learn from the list count syntax to practical Python list count examples, making it clear how to count items in a Python list effortlessly.

Table Of Contents:

What is the Count Method in Python lists? 

The count() is a list method in Python that returns the frequency or number of times a specified item occurs in a list. This is especially useful in data processing and data manipulation when you need to know the number of instances of certain elements returned in the list. The time complexity of count() runs in O(n).

Syntax of count() in Python:

list.count(element)

Here, the list.count(element) method is used to return the number of times the specified element occurs in the list and also checks for the exact matches based on values and case sensitivity. 

Example 1 of the count() method in Python:

Python

Output:

class(1)

Here is a Python code where we list some students in a list called students. Now, to count the occurrence of the string rahul, we can use the built-in method count() of lists. The statement, students.count(“rahul”) searches the list and returns occurrences of rahul. The result will be in the variable count. Lastly, the print() function prints the count and the occurrence of rahul in the list.

Example 2 of the count() method in Python:

Python

Output:

class(2)

In the above code, the courses list contains different courses. By using the count() method, we check the element that occurs multiple times. Here, the intellipaat occurs more than once and stores the result in count() when it is printed. 

Example 3 of the count() method in Python:

To count the number of pens ordered from the online store.

Python

Output:

count() python example 3 output

Explanation: Here, the count() function in Python counted the number of times the pen was ordered from the store.

Example 4 of the count() method in Python:

List Containing Different Datatypes

Python

Output:

count() python example 4 output

Explanation: The code has many data types together, like int, string and float, and by using print command we fetched the string Python from the list.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

To sum up, the count() method in Python offers a reliable and readable solution for frequency-related tasks in lists. It’s especially useful when you need to count occurrences in a Python list without adding unnecessary complexity to your code. Whether you’re handling large datasets or performing quick data validation, the Python list count method is an efficient tool. If you’re looking to understand how to count items in a Python list or just want a hands-on Python list count with example, this method delivers with simplicity and clarity. Explore more advanced tools, but for most needs, the Python count method remains a go-to solution.

You can learn more about Python in the Python Course and also explore Python Interview Questions prepared by industry experts.

other_methods_to_count_the_occurences_python_list

Other methods to count the occurrences in a Python list

  • collections.Counter in Python
  • Python List countOf() Method
  • Pandas value_counts() in Python Lists
  • How to Count List Elements with a For Loop in Python

The articles below cover important Python libraries and tools for efficient coding and development:

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner