How do i count multiple elements in a list python?

Below are the three solutions:

Fastest is using a for loop and storing it in a Dict.

import time
from collections import Counter


def countElement(a):
    g = {}
    for i in a:
        if i in g: 
            g[i] +=1
        else: 
            g[i] =1
    return g


z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]


#Solution 1 - Faster
st = time.monotonic()
for i in range(1000000):
    b = countElement(z)
et = time.monotonic()
print(b)
print('Simple for loop and storing it in dict - Duration: {}'.format(et - st))

#Solution 2 - Fast
st = time.monotonic()
for i in range(1000000):
    a = Counter(z)
et = time.monotonic()
print (a)
print('Using collections.Counter - Duration: {}'.format(et - st))

#Solution 3 - Slow
st = time.monotonic()
for i in range(1000000):
    g = dict([(i, z.count(i)) for i in set(z)])
et = time.monotonic()
print(g)
print('Using list comprehension - Duration: {}'.format(et - st))

Result

#Solution 1 - Faster
{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 234: 3, 23: 10, 12: 2, 123: 1, 31: 1, 13: 1, 42: 5, 34: 4, 423: 3}
Simple for loop and storing it in dict - Duration: 12.032000000000153
#Solution 2 - Fast
Counter({23: 10, 4: 6, 2: 5, 42: 5, 1: 4, 3: 4, 34: 4, 234: 3, 423: 3, 5: 2, 12: 2, 123: 1, 31: 1, 13: 1})
Using collections.Counter - Duration: 15.889999999999418
#Solution 3 - Slow
{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 34: 4, 423: 3, 234: 3, 42: 5, 12: 2, 13: 1, 23: 10, 123: 1, 31: 1}
Using list comprehension - Duration: 33.0

Understanding how to compute the number of occurrences of a specific list element in Python

How do i count multiple elements in a list python?

Photo by Steve Johnson on Unsplash

Introduction

Counting the number of occurrences of list elements in Python is definitely a fairly common task. In today’s short guide we will explore a few different ways for counting list items. More specifically, we will explore how to

  • count the number of occurrences of a single list item
  • count the occurrences of multiple list items

We will showcase how to achieve both using a specific list method or with the help of collections module.

First, let’s create an example list that we’ll reference throughout this guide in order to demonstrate a few concepts.

>>> my_lst = [1, 1, 2, 3, 1, 5, 6, 2, 3, 3]
>>> my_lst
[1, 1, 2, 3, 1, 5, 6, 2, 3, 3]

Counting the number of occurrences of a single list item

The first option you have when it comes to counting the number of times a particular element appears in a list, is list.count() method. For instance, in order to count how many times 1 appears in list my_lst you simply need to run the following command

>>> my_lst.count(1)
3

Alternatively, you can even use the Counter class that comes with the collections module. collections.Counter class is a subclass of dict that is used to count hashable objects. Essentially, it is a collection whether the elements for which counts are observed are stored as dictionary keys and their corresponding counts are stored as dictionary values.

>>> from collections import Counter
>>>
>>> Counter(my_lst)[1]
3

Counting the number of occurrences of multiple list items

Now if you want to count the number of times multiple items appear in a given list then again, you have a couple of options for doing so.

The first option you have is to call list.count() within a for-loop. Let’s say you want to count the number of times that list elements 1, 3 and 5 appear in our list. You can do so, as shown below:

>>> lookup = [1, 3, 5]
>>> [my_lst.count(e) for e in lookup]
[3, 3, 1]

Alternatively, you can use collections.Counter class which in my opinion is more suitable when it comes to counting the number of occurrences of multiple items.

If you want to count the number of times every single element appears in the list then you can simply create an instance of Counter.

>>> from collections import Counter
>>>
>>> Counter(my_lst)
Counter({1: 3, 2: 2, 3: 3, 5: 1, 6: 1})

If you want to count the number of times a subset of elements appear in the list then the following command will do the trick:

>>> from collections import Counter
>>>
>>> lookup = [1, 3, 5]
>>> all_counts = Counter(my_lst)
>>> counts = {k: all_counts[k] for k in lookup}

>>> counts
{1: 3, 3: 3, 5: 1}

Final Thoughts

In today’s short guide we explored a couple of different ways for counting the number of occurrences of list items in Python. We showcased how to use the list method count() or the collections.Counter class in order to count the occurrences of one or multiple list items.

Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

How do you count items in a list in Python?

We can use the len( ) function to return the number of elements present in the list.

How do you count the number of occurrences of an element in a list?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.

How do you count the number of repeated elements in a list Python?

Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.