Python

How to count the number of occurrences in a list in Python

April 15, 2022

How to count the number of occurrences in a list in Python

In this article you will learn how to count the number of occurrences in a list – that is, the number of times a value is repeated in a list in Python

count the number of occurrences in a list thumb

Hey guys, what’s up? Let’s learn more about Python and lists in today article! 😀

To count elements that are repeated, we will use the count method

On top of the list we are going to check the elements

Let’s see a practical example:

list = [1, 2, 3, 1, 4, 5, 6, 1, 7, 8]

ocurrences = list.count(1)

print(ocurrences)

Here we are checking in the list variable, how many times the number 1 is repeated, by the count method

When using print in the repetitions variable we have the number 3 being displayed

This is the number of 1s in the list

So it is with the count method that we have the possibility to see how many times an element is repeated in a list in Python

There are certainly other approaches, but count is the most practical and simple of all.

Conclusion

In this article we learned how to count the number of occurrences in a list in Python language

To count repeated elements, we use the count method in the list, passing as a parameter the element that we want to count the repetitions

We can display this number of repetitions using the print method

Do you want to learn more about Python? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x