Skip to main content
Update the performance section for a Python 2 / Python 3 world (timings from Python 3.6).
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k

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 xrangerange(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 xrangerange(n)]' ) print ("Counter(): ", t1.repeat(repeat=3,number=10000)) print ("count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6[0.36064818714657946062711701961234, 60.6138815596997564022796869976446, 60.392260466851987]3974247490405105] count(): [12[7.885062765334006779430688009597, 137.045601897769359962715800967999, 128.87746743077426]420845870045014] 

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:

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 xrange(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 xrange(n)]' ) print "Counter(): ", t1.repeat(repeat=3,number=10000) print "count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6.360648187146579, 6.613881559699756, 6.392260466851987] count(): [12.885062765334006, 13.045601897769359, 12.87746743077426] 

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] 
added single item case
Source Link
user2314737
  • 29.7k
  • 20
  • 109
  • 126

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.

countCounting 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} 

CounterCounting 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:

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 xrange(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 xrange(n)]' ) print "Counter(): ", t1.repeat(repeat=3,number=10000) print "count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6.360648187146579, 6.613881559699756, 6.392260466851987] count(): [12.885062765334006, 13.045601897769359, 12.87746743077426] 

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

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} 

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. 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:

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 xrange(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 xrange(n)]' ) print "Counter(): ", t1.repeat(repeat=3,number=10000) print "count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6.360648187146579, 6.613881559699756, 6.392260466851987] count(): [12.885062765334006, 13.045601897769359, 12.87746743077426] 

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:

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 xrange(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 xrange(n)]' ) print "Counter(): ", t1.repeat(repeat=3,number=10000) print "count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6.360648187146579, 6.613881559699756, 6.392260466851987] count(): [12.885062765334006, 13.045601897769359, 12.87746743077426] 
Source Link
user2314737
  • 29.7k
  • 20
  • 109
  • 126

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

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} 

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. 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:

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 xrange(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 xrange(n)]' ) print "Counter(): ", t1.repeat(repeat=3,number=10000) print "count(): ", t2.repeat(repeat=3,number=10000) 

And the output:

Counter(): [6.360648187146579, 6.613881559699756, 6.392260466851987] count(): [12.885062765334006, 13.045601897769359, 12.87746743077426]