Knowing how many times a specified number appears inside the lists 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 in that list, one can also count manually how many times a certain item is there through the elements of the list. 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’s different from another method 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:
Output:
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 inbuilt 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:
Output:
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