2125

Given a single item, how do I count occurrences of it in a list, in Python?


A related but different problem is counting occurrences of each different element in a collection, getting a dictionary or list as a histogram result instead of a single integer. For that problem, see Using a dictionary to count the items in a list.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
weakish
  • 28,682
  • 5
  • 48
  • 60
  • 2
    For reference, [this thread on meta](https://meta.stackoverflow.com/questions/419484/what-to-do-when-the-question-is-fine-but-existing-answers-need-more-focus-or-a) discussed the reason why both these threads remained open as-is. – metatoaster Jul 29 '22 at 00:25
  • 2
    As [discussed on meta](https://meta.stackoverflow.com/questions/419484/what-should-i-do-when-the-question-is-fine-but-existing-answers-need-more-focu), this question is nominally about counting a single item. Over the years, it's attracted multiple (good) answers about the related but significantly different problem of histogramming; counting every different element, which should use an O(n) algorithm, not `.count` for every element which is O(n^2). Ideally those should be in (a) different Q&A(s), but for now they're here. – Peter Cordes Jul 30 '22 at 02:20

30 Answers30

2470

If you only want a single item's count, use the count method:

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3

Important: this is very slow if you are counting multiple different items

Each count call goes over the entire list of n elements. Calling count in a loop n times means n * n total checks, which can be catastrophic for performance.

If you want to count multiple items, use Counter, which only does n total checks.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Łukasz
  • 35,061
  • 4
  • 33
  • 33
  • 19
    `mylist = [1,7,7,7,3,9,9,9,7,9,10,0] print sorted(set([i for i in mylist if mylist.count(i)>2]))` – cpp-coder Sep 09 '17 at 19:15
  • 7
    Counting unique elements in my case yielded the following timings: **114.19**seconds with `list.count()`, **0.53** seconds with `numpy.unique(list, return_counts = True)` and **0.17** seconds with `Counter`. The difference is striking. – Konstantinos Feb 10 '22 at 19:14
2375

Use Counter if you are using Python 2.7 or 3.x and you want the number of occurrences for each element:

>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user52028778
  • 27,164
  • 3
  • 36
  • 42
  • 4
    I have found that when using this a lot (talking about millions of strings) that it is very slow because of its calls to `isinstance`. So if you are certain about the data that you're working with, it might be better to write a custom function without type and instance checking. – Bram Vanroy Jun 19 '18 at 21:26
  • 8
    @BramVanroy: What `isinstance` calls? Even with millions of strings, calling `Counter` only involves one `isinstance` call, to check whether its argument is a mapping. You most likely misjudged what's eating all your time. – user2357112 Nov 14 '18 at 04:08
  • 1
    You misinterpreted what I meant: Counter checks the types of your data before it creates the Counter. This takes relatively much time and if you know the type of your data in advance. If you look at Counter's update method, you'll see it has to go through three if-statements before doing something. If you call update frequently, this adds up quickly. When you have control over your data and you *know* that the input will be indeed an iterable, then you can skip the first two checks. As I said, I only noticed this when working with millions of updates so it's an edge case. – Bram Vanroy Nov 14 '18 at 08:45
  • 9
    @BramVanroy: If you're performing millions of *updates* rather than just counting millions of strings, that's a different story. The optimization effort in `Counter` has gone into counting large iterables, rather than counting many iterables. Counting a million-string iterable will go faster with `Counter` than with a manual implementation. If you want to call `update` with many iterables, you may be able to speed things up by joining them into one iterable with `itertools.chain`. – user2357112 Nov 14 '18 at 18:20
  • 17
    In case you want to sort the results [how-to-sort-counter-by-value-python](https://stackoverflow.com/questions/20950650/how-to-sort-counter-by-value-python) --> `x = Counter({'a':5, 'b':3, 'c':7})` `x.most_common()` – Javi Dec 22 '20 at 09:27
  • 1
    When the large iterable is a column of a pandas data frame (in my case ~0.5M of strings), I have found pandas' `value_counts` method to be orders of magnitude faster than Counter. – Antoine Aug 11 '21 at 11:05
  • And to have the dict instead of Counter, `dict(Counter(z))`. – Mehmet Burak Sayıcı Dec 19 '21 at 23:08
358

Counting the occurrences of one item in a list

For counting the occurrences of just one list item you can use count()

>>> l = ["a","b","b"]
>>> l.count("a")
1
>>> l.count("b")
2

Counting the occurrences of all items in a list is also known as "tallying" a list, or creating a tally counter.

Counting all items with count()

To count the occurrences of items in l one can simply use a list comprehension and the count() method

[[x,l.count(x)] for x in set(l)]

(or similarly with a dictionary dict((x,l.count(x)) for x in set(l)))

Example:

>>> l = ["a","b","b"]
>>> [[x,l.count(x)] for x in set(l)]
[['a', 1], ['b', 2]]
>>> dict((x,l.count(x)) for x in set(l))
{'a': 1, 'b': 2}

Counting all items with Counter()

Alternatively, there's the faster Counter class from the collections library

Counter(l)

Example:

>>> l = ["a","b","b"]
>>> from collections import Counter
>>> Counter(l)
Counter({'b': 2, 'a': 1})

How much faster is Counter?

I checked how much faster Counter is for tallying lists. I tried both methods out with a few values of n and it appears that Counter is faster by a constant factor of approximately 2.

Here is the script I used:

from __future__ import print_function
import timeit

t1=timeit.Timer('Counter(l)', \
                'import random;import string;from collections import Counter;n=1000;l=[random.choice(string.ascii_letters) for x in range(n)]'
                )

t2=timeit.Timer('[[x,l.count(x)] for x in set(l)]',
                'import random;import string;n=1000;l=[random.choice(string.ascii_letters) for x in range(n)]'
                )

print("Counter(): ", t1.repeat(repeat=3,number=10000))
print("count():   ", t2.repeat(repeat=3,number=10000)

And the output:

Counter():  [0.46062711701961234, 0.4022796869976446, 0.3974247490405105]
count():    [7.779430688009597, 7.962715800967999, 8.420845870045014]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2314737
  • 27,088
  • 20
  • 102
  • 114
  • 50
    `Counter` is *way* faster for bigger lists. The list comprehension method is O(n^2), `Counter` should be O(n). – fhucho Nov 11 '15 at 22:34
  • 45
    Counter is not faster by a factor of 2, Counter is faster by a *factor of n* (O(n^2) vs O(n)). – Martijn Pieters May 23 '17 at 10:13
  • 2
    I have found that when using this a lot (talking about millions of strings) that it is very slow because of its calls to `isinstance`. So if you are certain about the data that you're working with, it might be better to write a custom function without type and instance checking. – Bram Vanroy Jun 19 '18 at 21:27
85

Another way to get the number of occurrences of each item, in a dictionary:

dict((i, a.count(i)) for i in a)
the
  • 21,007
  • 11
  • 68
  • 101
tj80
  • 971
  • 6
  • 2
  • 61
    this looks like one of the constructs I often come up with in the heat of the battle, but it will run through a len(a) times which means quadratic runtime complexity (as each run depends on len(a) again). – Nicolas78 Oct 10 '12 at 00:30
  • 6
    would dict((i,a.count(i)) for i in set(a)) be more correct and faster? – hugo24 Aug 23 '13 at 09:20
  • 9
    @hugo24: A bit, but it won't be asymptotically faster in the worst case; it will take `n * (number of different items)` operations, not counting the time it takes to build the set. Using `collections.Counter` is really much better. – Clément Oct 07 '13 at 09:46
  • very late to the party but wouldn't following code throw an error if a list contained more than one instance of `i`, because it will try to enter multiple keys of same value in a dictionary. `dict((i, a.count(i)) for i in a)` – rp1 Sep 16 '19 at 03:07
  • @rp1 you can try it for yourself and see that later key-value pairs just overwrite the previous entry for the same key, for example `dict([(1, 2), (1, 3)])` returns `{1: 3}` – xuiqzy Nov 15 '20 at 22:08
66

Given an item, how can I count its occurrences in a list in Python?

Here's an example list:

>>> l = list('aaaaabbbbcccdde')
>>> l
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

list.count

There's the list.count method

>>> l.count('b')
4

This works fine for any list. Tuples have this method as well:

>>> t = tuple('aabbbffffff')
>>> t
('a', 'a', 'b', 'b', 'b', 'f', 'f', 'f', 'f', 'f', 'f')
>>> t.count('f')
6

collections.Counter

And then there's collections.Counter. You can dump any iterable into a Counter, not just a list, and the Counter will retain a data structure of the counts of the elements.

Usage:

>>> from collections import Counter
>>> c = Counter(l)
>>> c['b']
4

Counters are based on Python dictionaries, their keys are the elements, so the keys need to be hashable. They are basically like sets that allow redundant elements into them.

Further usage of collections.Counter

You can add or subtract with iterables from your counter:

>>> c.update(list('bbb'))
>>> c['b']
7
>>> c.subtract(list('bbb'))
>>> c['b']
4

And you can do multi-set operations with the counter as well:

>>> c2 = Counter(list('aabbxyz'))
>>> c - c2                   # set difference
Counter({'a': 3, 'c': 3, 'b': 2, 'd': 2, 'e': 1})
>>> c + c2                   # addition of all elements
Counter({'a': 7, 'b': 6, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c | c2                   # set union
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c & c2                   # set intersection
Counter({'a': 2, 'b': 2})

Silly answer, sum

There are good builtin answers, but this example is slightly instructive. Here we sum all the occurences where the character, c, is equal to 'b':

>>> sum(c == 'b' for c in l)
4

Not great for this use-case, but if you need to have a count of iterables where the case is True it works perfectly fine to sum the boolean results, since True is equivalent to 1.

Why not pandas?

Another answer suggests:

Why not use pandas?

Pandas is a common library, but it's not in the standard library. Adding it as a requirement is non-trivial.

There are builtin solutions for this use-case in the list object itself as well as in the standard library.

If your project does not already require pandas, it would be foolish to make it a requirement just for this functionality.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 8
    While "why not Pandas" is appropriate, it should probably be accompanied by "when to use NumPy", i.e. for large numeric arrays. The deciding factor isn't just project limitations, there are memory efficiencies with NumPy which become apparent with big data. – jpp Jan 03 '19 at 01:36
  • 2
    Thanks for mentioning Pandas/etc as a serious dependency. Some of these packages have negative side effects. So addition of these assets for trivial needs can cost a lot of time and $. Personally I have experienced Numpy and SciPi adding 30 min to our CI pipeline and it took days to get the package caching correctly. Great packages, but sometimes there is hidden expense. +1'd – Marc Nov 13 '19 at 00:24
60

I've compared all suggested solutions (and a few new ones) with perfplot (a small project of mine).

Counting one item

For large enough arrays, it turns out that

numpy.sum(numpy.array(a) == 1)

is slightly faster than the other solutions.

enter image description here

Counting all items

As established before,

numpy.bincount(a)

is what you want.

enter image description here


Code to reproduce the plots:

from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot


def counter(a):
    return Counter(a)


def count(a):
    return dict((i, a.count(i)) for i in set(a))


def bincount(a):
    return numpy.bincount(a)


def pandas_value_counts(a):
    return pandas.Series(a).value_counts()


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


def count_unsorted_list_items(items):
    counts = defaultdict(int)
    for item in items:
        counts[item] += 1
    return dict(counts)


def operator_countof(a):
    return dict((i, operator.countOf(a, i)) for i in set(a))


perfplot.show(
    setup=lambda n: list(numpy.random.randint(0, 100, n)),
    n_range=[2**k for k in range(20)],
    kernels=[
        counter, count, bincount, pandas_value_counts, occur_dict,
        count_unsorted_list_items, operator_countof
        ],
    equality_check=None,
    logx=True,
    logy=True,
    )
from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot


def counter(a):
    return Counter(a)


def count(a):
    return dict((i, a.count(i)) for i in set(a))


def bincount(a):
    return numpy.bincount(a)


def pandas_value_counts(a):
    return pandas.Series(a).value_counts()


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


def count_unsorted_list_items(items):
    counts = defaultdict(int)
    for item in items:
        counts[item] += 1
    return dict(counts)


def operator_countof(a):
    return dict((i, operator.countOf(a, i)) for i in set(a))


b = perfplot.bench(
    setup=lambda n: list(numpy.random.randint(0, 100, n)),
    n_range=[2 ** k for k in range(20)],
    kernels=[
        counter,
        count,
        bincount,
        pandas_value_counts,
        occur_dict,
        count_unsorted_list_items,
        operator_countof,
    ],
    equality_check=None,
)
b.save("out.png")
b.show()
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 11
    numpy.bincount() will work only for lists with int items. – Mukarram Pasha Mar 03 '18 at 09:20
  • The first program doesn't actually measure counting one item, does it? Looks like it's doing the same as the second program. Can you check/fix that? And I think `numpy.random.randint(0, 100, n).tolist()` would be better. With your `list(numpy.random.randint(0, 100, n))` you have NumPy ints in a Python list, which seems weird/unrealistic. – Kelly Bundy Feb 06 '22 at 21:23
56

list.count(x) returns the number of times x appears in a list

see: http://docs.python.org/tutorial/datastructures.html#more-on-lists

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
43

If you want to count all values at once you can do it very fast using numpy arrays and bincount as follows

import numpy as np
a = np.array([1, 2, 3, 4, 1, 4, 1])
np.bincount(a)

which gives

>>> array([0, 3, 1, 1, 2])
flonk
  • 3,726
  • 3
  • 24
  • 37
  • Downsides: `bincount` works only on non-negative `int`s, and it counts also all "missing values", so that the size of the result is `1+ max(a)`, which is quite large even if `a` contains only the **Julian Day** of today, 2459893. – Rainald62 Nov 10 '22 at 21:55
28

Why not using Pandas?

import pandas as pd

my_list = ['a', 'b', 'c', 'd', 'a', 'd', 'a']

# converting the list to a Series and counting the values
my_count = pd.Series(my_list).value_counts()
my_count

Output:

a    3
d    2
b    1
c    1
dtype: int64

If you are looking for a count of a particular element, say a, try:

my_count['a']

Output:

3
Community
  • 1
  • 1
Shoresh
  • 2,693
  • 2
  • 16
  • 9
28

If you can use pandas, then value_counts is there for rescue.

>>> import pandas as pd
>>> a = [1, 2, 3, 4, 1, 4, 1]
>>> pd.Series(a).value_counts()
1    3
4    2
3    1
2    1
dtype: int64

It automatically sorts the result based on frequency as well.

If you want the result to be in a list of list, do as below

>>> pd.Series(a).value_counts().reset_index().values.tolist()
[[1, 3], [4, 2], [3, 1], [2, 1]]
Thirupathi Thangavel
  • 2,418
  • 3
  • 29
  • 49
  • 1
    pandas has lots of overhead, though, so it's the slowest solution with small amounts of data. https://stackoverflow.com/a/46195192/125507 – endolith Aug 20 '19 at 14:56
15

I had this problem today and rolled my own solution before I thought to check SO. This:

dict((i,a.count(i)) for i in a)

is really, really slow for large lists. My solution

def occurDict(items):
    d = {}
    for i in items:
        if i in d:
            d[i] = d[i]+1
        else:
            d[i] = 1
return d

is actually a bit faster than the Counter solution, at least for Python 2.7.

D Blanc
  • 167
  • 1
  • 2
  • 1
    Counter sorts the entries while yours does not, hence the speed difference (True at the time of writing, not sure if it was when you wrote the answer. Still, it might be relevant for someone scrolling down.) – chaosflaws Jun 08 '15 at 21:29
  • 7
    Counter in Python 2 was a little on the slow side, yes. It uses C-optimised code to do the counting in Python 3 however, and now beats your loop with ease. – Martijn Pieters Apr 22 '17 at 18:05
14

Count of all elements with itertools.groupby()

Antoher possiblity for getting the count of all elements in the list could be by means of itertools.groupby().

With "duplicate" counts

from itertools import groupby

L = ['a', 'a', 'a', 't', 'q', 'a', 'd', 'a', 'd', 'c']  # Input list

counts = [(i, len(list(c))) for i,c in groupby(L)]      # Create value-count pairs as list of tuples 
print(counts)

Returns

[('a', 3), ('t', 1), ('q', 1), ('a', 1), ('d', 1), ('a', 1), ('d', 1), ('c', 1)]

Notice how it combined the first three a's as the first group, while other groups of a are present further down the list. This happens because the input list L was not sorted. This can be a benefit sometimes if the groups should in fact be separate.

With unique counts

If unique group counts are desired, just sort the input list:

counts = [(i, len(list(c))) for i,c in groupby(sorted(L))]
print(counts)

Returns

[('a', 5), ('c', 1), ('d', 2), ('q', 1), ('t', 1)]

Note: For creating unique counts, many of the other answers provide easier and more readable code compared to the groupby solution. But it is shown here to draw a parallel to the duplicate count example.

Tim Skov Jacobsen
  • 3,583
  • 4
  • 26
  • 23
14

Although it is very old question, since i didn't find a one liner, i made one.

# original numbers in list
l = [1, 2, 2, 3, 3, 3, 4]

# empty dictionary to hold pair of number and its count
d = {}

# loop through all elements and store count
[ d.update( {i:d.get(i, 0)+1} ) for i in l ]

print(d)
# {1: 1, 2: 2, 3: 3, 4: 1}
Harsh Gundecha
  • 1,139
  • 9
  • 16
  • 3
    Don't use list comprehensions for side effects. See: [Is it Pythonic to use list comprehensions for just side effects?](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects) – Georgy Jul 04 '19 at 07:56
13
# Python >= 2.6 (defaultdict) && < 2.7 (Counter, OrderedDict)
from collections import defaultdict
def count_unsorted_list_items(items):
    """
    :param items: iterable of hashable items to count
    :type items: iterable

    :returns: dict of counts like Py2.7 Counter
    :rtype: dict
    """
    counts = defaultdict(int)
    for item in items:
        counts[item] += 1
    return dict(counts)


# Python >= 2.2 (generators)
def count_sorted_list_items(items):
    """
    :param items: sorted iterable of items to count
    :type items: sorted iterable

    :returns: generator of (item, count) tuples
    :rtype: generator
    """
    if not items:
        return
    elif len(items) == 1:
        yield (items[0], 1)
        return
    prev_item = items[0]
    count = 1
    for item in items[1:]:
        if prev_item == item:
            count += 1
        else:
            yield (prev_item, count)
            count = 1
            prev_item = item
    yield (item, count)
    return


import unittest
class TestListCounters(unittest.TestCase):
    def test_count_unsorted_list_items(self):
        D = (
            ([], []),
            ([2], [(2,1)]),
            ([2,2], [(2,2)]),
            ([2,2,2,2,3,3,5,5], [(2,4), (3,2), (5,2)]),
            )
        for inp, exp_outp in D:
            counts = count_unsorted_list_items(inp) 
            print inp, exp_outp, counts
            self.assertEqual(counts, dict( exp_outp ))

        inp, exp_outp = UNSORTED_WIN = ([2,2,4,2], [(2,3), (4,1)])
        self.assertEqual(dict( exp_outp ), count_unsorted_list_items(inp) )


    def test_count_sorted_list_items(self):
        D = (
            ([], []),
            ([2], [(2,1)]),
            ([2,2], [(2,2)]),
            ([2,2,2,2,3,3,5,5], [(2,4), (3,2), (5,2)]),
            )
        for inp, exp_outp in D:
            counts = list( count_sorted_list_items(inp) )
            print inp, exp_outp, counts
            self.assertEqual(counts, exp_outp)

        inp, exp_outp = UNSORTED_FAIL = ([2,2,4,2], [(2,3), (4,1)])
        self.assertEqual(exp_outp, list( count_sorted_list_items(inp) ))
        # ... [(2,2), (4,1), (2,1)]
Wes Turner
  • 1,211
  • 13
  • 15
12

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
Akash Swain
  • 520
  • 3
  • 13
  • Rather Use Counter as mentioned in the solution above by @user52028778 – Kishan K Nov 23 '20 at 10:51
  • 2
    @KishanK If you see my answer, I have tried with Counter(Solution 2) as well, but still the Solution1 using the loop runs faster than it. – Akash Swain Nov 24 '20 at 06:30
  • @AkashSwain In the method you implemented I guess you could make it run a bit faster by removing already counted elements from the list as the for loop wouldn't have to check those... I would copy the list and then remove from the copied list and do the for loop, also I guess you would have to do the loop in reverse order since you would be removing things from the list... – loco.loop Sep 15 '21 at 00:23
9

It was suggested to use numpy's bincount, however it works only for 1d arrays with non-negative integers. Also, the resulting array might be confusing (it contains the occurrences of the integers from min to max of the original list, and sets to 0 the missing integers).

A better way to do it with numpy is to use the unique function with the attribute return_counts set to True. It returns a tuple with an array of the unique values and an array of the occurrences of each unique value.

# a = [1, 1, 0, 2, 1, 0, 3, 3]
a_uniq, counts = np.unique(a, return_counts=True)  # array([0, 1, 2, 3]), array([2, 3, 1, 2]

and then we can pair them as

dict(zip(a_uniq, counts))  # {0: 2, 1: 3, 2: 1, 3: 2}

It also works with other data types and "2d lists", e.g.

>>> a = [['a', 'b', 'b', 'b'], ['a', 'c', 'c', 'a']]
>>> dict(zip(*np.unique(a, return_counts=True)))
{'a': 3, 'b': 3, 'c': 2}
Andreas K.
  • 9,282
  • 3
  • 40
  • 45
6

You can also use countOf method of a built-in module operator.

>>> import operator
>>> operator.countOf([1, 2, 3, 4, 1, 4, 1], 1)
3
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
  • 2
    How is `countOf` is implemented? How does it compare to the more obvious `list.count` (which benefits from C implementation)? Are there any advantages? – Chris_Rands May 23 '17 at 09:41
6

To count the number of diverse elements having a common type:

li = ['A0','c5','A8','A2','A5','c2','A3','A9']

print sum(1 for el in li if el[0]=='A' and el[1] in '01234')

gives

3 , not 6

eyquem
  • 26,771
  • 7
  • 38
  • 46
5

I would use filter(), take Lukasz's example:

>>> lst = [1, 2, 3, 4, 1, 4, 1]
>>> len(filter(lambda x: x==1, lst))
3
IPython
  • 91
  • 1
  • 9
5

use %timeit to see which operation is more efficient. np.array counting operations should be faster.

 from collections import Counter
 mylist = [1,7,7,7,3,9,9,9,7,9,10,0] 
 types_counts=Counter(mylist)
 print(types_counts)
Golden Lion
  • 3,840
  • 2
  • 26
  • 35
4

May not be the most efficient, requires an extra pass to remove duplicates.

Functional implementation :

arr = np.array(['a','a','b','b','b','c'])
print(set(map(lambda x  : (x , list(arr).count(x)) , arr)))

returns :

{('c', 1), ('b', 3), ('a', 2)}

or return as dict :

print(dict(map(lambda x  : (x , list(arr).count(x)) , arr)))

returns :

{'b': 3, 'c': 1, 'a': 2}
blue-sky
  • 51,962
  • 152
  • 427
  • 752
4

Given a list X

 import numpy as np
 X = [1, -1, 1, -1, 1]

The dictionary which shows i: frequency(i) for elements of this list is:

{i:X.count(i) for i in np.unique(X)}

Output:

{-1: 2, 1: 3}
4
mot = ["compte", "france", "zied"]
lst = ["compte", "france", "france", "france", "france"]
dict((x, lst.count(x)) for x in set(mot))

this gives

{'compte': 1, 'france': 4, 'zied': 0}
ben othman zied
  • 176
  • 1
  • 7
  • This worked for me, though the additional de-duped 'mot' variable is redundant because of the set function, so this works also: lst = ["compte", "france", "france", "france", "france"] dict((x, lst.count(x)) for x in set(lst)) – sean May 22 '22 at 23:15
3

Alternatively, you can also implement the counter by yourself. This is the way I do:

item_list = ['me', 'me', 'you', 'you', 'you', 'they']

occ_dict = {}

for item in item_list:
    if item not in occ_dict:
        occ_dict[item] = 1
    else:
        occ_dict[item] +=1

print(occ_dict)

Output: {'me': 2, 'you': 3, 'they': 1}

Abel Rodríguez
  • 474
  • 5
  • 6
2
sum([1 for elem in <yourlist> if elem==<your_value>])

This will return the amount of occurences of your_value

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
-1
test = [409.1, 479.0, 340.0, 282.4, 406.0, 300.0, 374.0, 253.3, 195.1, 269.0, 329.3, 250.7, 250.7, 345.3, 379.3, 275.0, 215.2, 300.0]

for i in test:
    print('{} numbers {}'.format(i, test.count(i)))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ash
  • 25
  • 4
-1
import pandas as pd
test = [409.1, 479.0, 340.0, 282.4, 406.0, 300.0, 374.0, 253.3, 195.1, 269.0, 329.3, 250.7, 250.7, 345.3, 379.3, 275.0, 215.2, 300.0]

#turning the list into a temporary dataframe
test  = pd.DataFrame(test)

#using the very convenient value_counts() function
df_counts = test.value_counts()
df_counts

then you can use df_counts.index and df_counts.values to get the data.

Mehdi Fekih
  • 95
  • 1
  • 2
-1
x = ['Jess', 'Jack', 'Mary', 'Sophia', 'Karen',
     'Addison', 'Joseph','Jack', 'Jack', 'Eric', 'Ilona', 'Jason']
the_item = input('Enter the item that you wish to find : ')
how_many_times = 0 
for occurrence in x:
     if occurrence == the_item : 
          how_many_times += 1
print('The occurrence of', the_item, 'in', x,'is',how_many_times) 

Created a list of names wherein the name 'Jack' is repeated. In order to check its Occurrence, I ran a for loop in the list named x. Upon each iteration, if the loop variable attains the value same that of received from the user and stored in the variable the_item, the variable how_many_times gets incremented by 1. After attaining some value...We print how_many_times which stores the value of the occurance of the word 'jack'

LunaticXXD10
  • 421
  • 4
  • 10
-1

If you only want to get a single item's count, use the python in-built count method:

list = [2,5,4,3,10,3,5,6,5]
elmenet_count = list.count(5)
print('The count of element 5 is ', elmenet_count )

Output:

The count of element 5 is 3
-2
def countfrequncyinarray(arr1):
    r=len(arr1)
    return {i:arr1.count(i) for i in range(1,r+1)}
arr1=[4,4,4,4]
a=countfrequncyinarray(arr1)
print(a)
ravi tanwar
  • 598
  • 5
  • 16
  • 5
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Alex Riabov Jul 07 '18 at 08:53
  • This solution does not actually work; the input must be constructed in a very specific way to seemingly work. For example, `countfrequncyinarray([4, 4, 4])` will fail to count the `4`s. There is just no way to construct an input to count a `-1`, `sys.maxsize + 1` or similar entry. – MisterMiyagi Jul 30 '22 at 12:30