85

I am new to Python. I am trying to find a simple way of getting a count of the number of elements repeated in a list e.g.

MyList = ["a", "b", "a", "c", "c", "a", "c"]

Output:

a: 3
b: 1
c: 3
galoget
  • 722
  • 9
  • 15
Jojo
  • 912
  • 1
  • 7
  • 8

5 Answers5

199

You can do that using count:

my_dict = {i:MyList.count(i) for i in MyList}

>>> print my_dict     #or print(my_dict) in python-3.x
{'a': 3, 'c': 3, 'b': 1}

Or using collections.Counter:

from collections import Counter

a = dict(Counter(MyList))

>>> print a           #or print(a) in python-3.x
{'a': 3, 'c': 3, 'b': 1}
djvg
  • 11,722
  • 5
  • 72
  • 103
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • first option result in syntax error – Peter Kelly Apr 23 '14 at 10:16
  • 2
    It raises a Syntax Error in Python 3 (print is a function), it works in 2.7. – jabaldonedo Apr 23 '14 at 10:19
  • @sshashank124 ran it against 2.6.6, should have said that sorry - nevermind! – Peter Kelly Apr 23 '14 at 10:21
  • yeah was actually complaining about for loop in the literal dict in 2.6 (see my answer for 2.6 way, not that it is relevant...) – Peter Kelly Apr 23 '14 at 10:21
  • 4
    This solution is super slow. Much faster using pandas: `pd.DataFrame(MyList, columns=["x"]).groupby('x').size().to_dict()` – Make42 Jul 10 '17 at 15:19
  • Using the `.count` solution is *horribly inefficient* and an antipattern – juanpa.arrivillaga Feb 23 '20 at 21:46
  • Both of the above methods break during really big data [like the this one](https://hr-testcases-us-east-1.s3.amazonaws.com/9060/input00.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1582846628&Signature=TF4q914E8%2F6O0T9nFFD2UqArYdU%3D&response-content-type=text%2Fplain) – Nischaya Sharma Feb 27 '20 at 21:50
  • I actually have never been so happy as I was finding this answer! Solved my problem perfectly. 6 years later still just as fresh :) – BrettJ Dec 11 '20 at 08:36
  • 2
    if you use a 'set' for get unique values in the 'for' it will go faster: `setList = list(set(Mylist)) my_dict = {i:MyList.count(i) for i in setList}` – Eduardo Bocarruido Jun 07 '21 at 14:45
  • I recommend adding an efficiency comment in the actual answer for when using 'collections.Counter' vs '.count'. In larger datasets, it makes a huge difference – brw59 Mar 29 '22 at 22:23
26

Use Counter

>>> from collections import Counter
>>> MyList = ["a", "b", "a", "c", "c", "a", "c"]
>>> c = Counter(MyList)
>>> c
Counter({'a': 3, 'c': 3, 'b': 1})
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
12

This works for Python 2.6.6

a = ["a", "b", "a"]
result = dict((i, a.count(i)) for i in a)
print result

prints

{'a': 2, 'b': 1}
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
7
yourList = ["a", "b", "a", "c", "c", "a", "c"]

expected outputs {a: 3, b: 1,c:3}

duplicateFrequencies = {}
for i in set(yourList):
    duplicateFrequencies[i] = yourList.count(i)

Cheers!! Reference

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
7
In [2]: MyList = ["a", "b", "a", "c", "c", "a", "c"]

In [3]: count = {}

In [4]: for i in MyList:
   ...:     if not i in count:
   ...:         count[i] = 1
   ...:     else:
   ...:         count[i] +=1
   ...:

In [5]: count
Out[5]: {'a': 3, 'b': 1, 'c': 3}
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81