python count repeated elements in list code example
Example 1: count the duplicates in a list in python
some_list=['a','b','c','b','d','m','n','n']
my_list=sorted(some_list)
duplicates=[]
for i in my_list:
if my_list.count(i)>1:
if i not in duplicates:
duplicates.append(i)
print(duplicates)
Example 2: count similar values in list python
MyList = ["a", "b", "a", "c", "c", "a", "c"]
return my_dict = {i:MyList.count(i) for i in MyList}
{'a': 3, 'c': 3, 'b': 1}
from collections import Counter
return my_dict = dict(Counter(MyList))
{'a': 3, 'c': 3, 'b': 1}
Example 3: python count repeated elements in a list
dict_of_counts = {item:your_list.count(item) for item in your_list}
your_list = ["a", "b", "a", "c", "c", "a", "c"]
dict_of_counts = {item:your_list.count(item) for item in your_list}
print(dict_of_counts)
--> {'a': 3, 'b': 1, 'c': 3}
Example 4: How to see how many times somting is in a list python
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
count = vowels.count('i')
Example 5: count number of repeats in list python
mylist.count(element)
Example 6: how to check if there are duplicates in a list python
>>> your_list = ['one', 'two', 'one']
>>> len(your_list) != len(set(your_list))
True