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
6759 silver badges15 bronze badges
asked Apr 23, 2014 at 10:00
0
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
8,9664 gold badges56 silver badges89 bronze badges
answered Apr 23, 2014 at 10:01
sshashank124sshashank124
30.3k8 gold badges63 silver badges75 bronze badges
10
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}]
answered Apr 23, 2014 at 10:08
Jayanth KoushikJayanth Koushik
9,1461 gold badge39 silver badges49 bronze badges
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}
answered Apr 23, 2014 at 10:19
Peter KellyPeter Kelly
14k6 gold badges52 silver badges62 bronze badges
1
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
answered Apr 23, 2014 at 10:06
Daniel AdenewDaniel Adenew
7,3037 gold badges54 silver badges75 bronze badges
1
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}
answered Apr 23, 2014 at 10:08
If you want to count duplicates for a given element then use the count[] function. Use a counter[] function or basics logic combination to find all duplicated elements in a list and count them in Python.
Simple example code.
Using count[]
Get the occurrence of a given element in the List. Count “b” in the list.
MyList = ["b", "a", "a", "c", "b", "a", "c", 'a']
res = MyList.count['b']
print[res]
Output: 2
And if want to count each of the elements in the List using for loop.
MyList = ["b", "a", "a", "c", "b", "a", "c", 'a']
res = {}
for i in MyList:
res[i] = MyList.count[i]
print[res]
Output:
Same code using list comprehension
MyList = ["b", "a", "a", "c", "b", "a", "c", 'a']
res = {i:MyList.count[i] for i in MyList}
print[res]
Using collections.Counter[]
You need to import Counter from the collection.
from collections import Counter
MyList = ["a", "b", "a", "c", "c", "a", "c"]
res = Counter[MyList]
print[res]
print[res['a']]
Output:
Counter[{‘a’: 3, ‘c’: 3, ‘b’: 1}]
3
Do comment if you have any doubts and suggestions on this Python list topic.
Note: IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.