How to Count List Elements with a For Loop in Python

How-to-Count-List-Elements-with-a-For-Loop-in-Python-feature.jpg

Knowing how many times a specified number appears inside a list with a for loop is an operational procedure. To apply this method, it is quite simple to introduce a count variable and increase it whenever the same number is found inside the list. You will write a lot more code than built-in functions with this method, but it will allow you to do different operations (e.g., add another condition or even count multiple elements at once).

This is also more useful for big data with custom filters or in case of non-strict formatting of the results (in other words, when results do not only regard the number of unique value occurrences). You can always follow this approach. It’s a classic way that leads you into Python, from the main concepts of iteration and list traversal, through such concepts. 

Using a For Loop to Count Occurrences in a List

We are using a for loop so that one can also manually count the number of times a certain item appears in the list through its elements. That is, inside the loop, we just keep on checking for those sets of finding the target element, and for every successful find, increment a counter variable. It is different from other methods like count(), but it gives more flexibility in the sense that we can also add different conditions, or even at the same time, go for all the counts of multiple elements.

Usually, it is best to apply the complexity of data, filtering generally or in particular, a bit more complicated when combined with simpler tasks.

Example 1:

Python

Output:

count using for-loop(1)

Here is the explanation for the above code. The code is used to find out the number of occurrences of ‘rahul’ in the list without using the built-in count() method. We do this by defining a variable count as 0. And then, we have a for loop that goes through each student in the students list. Within the loop, we have an if condition that checks if the current element is equal to rahul. For every match, we increase the count variable by one. Finally, after the for loop, we print out the total occurrences of the string rahul in the list.

Example 2:

Python

Output:

count using for-loop(2)

In the above code, the courses list contains the course names and iterates using a for-loop. The for-loop checks if each element is ‘intellipaat’. If the condition is true, it increments the counter, and lastly, it prints the total number of occurrences of intellipaat in the list.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion 

In Python, there is a built-in method that can count the occurrences of an element. We could simply use a for loop to properly count the occurrences in a list with a counter. It is more flexible because one can add more conditions. In cases where built-in methods may not be sufficient, for example, working with complex data structures or applying custom filtering, it is useful. In such a manner, it allows the programmer to work with such exhibitions as list iteration, conditional statements, and traversing through lists in Python. 

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

Some of the other methods to count the occurrences in a Python list

  • Pandas value_counts() in Python Lists
  • Python List countOf() Method
  • collections.Counter in Python
  • Python List Count() Method

Explore key Python libraries and tools in the following articles to enhance your programming skills.-

ctypes Module in Python – Learn how the ctypes module works in Python through this article.

Python List Count Method – Learn how to use the count() method on lists in Python through this article.

Collections Counter in Python – Learn how the collections.Counter works in Python through this article.

Fix Python.h No Such File Error – Learn how to resolve the “python.h not found” error in Python with the help of this article.

Matplotlib Subplot in Python – Learn how to create and manage subplots using Matplotlib in Python through this article.

Measure Elapsed Time in Python – Learn how to track elapsed time in Python with the help of this article.

re.search() vs re.match() – Learn how re.search() and re.match() differ in behavior through this article.

FAQs on How to Count List Elements with a For Loop in Python

Q1. What’s the difference between a for loop and the count() method?

The count() method is easier and faster, but a for loop gives you more control — like adding conditions or counting multiple things.

Q2. Can I count more than one element at the same time?

Yes. You can use a dictionary or the Counter class from the collections module.

Q3. Which one is faster, for loop or Counter?

Counter is faster, but the for loop is better if you need custom logic.

Q4. Is using a for loop good for big lists?

It works fine, but for big data, Counter or Pandas methods are faster.

Q5. What’s the easiest way to count in Python?

For simple counting, we can use list.count(), and for more advanced counting, we can use collections.Counter.

About the Author

Software Developer | Technical Research Analyst Lead | Full Stack & Cloud Systems

Ayaan Alam is a skilled Software Developer and Technical Research Analyst Lead with 2 years of professional experience in Java, Python, and C++. With expertise in full-stack development, system design, and cloud computing, he consistently delivers high-quality, scalable solutions. Known for producing accurate and insightful technical content, Ayaan contributes valuable knowledge to the developer community.

Full Stack Developer Course Banner