python count elements in list code example
Example 1: 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 2: how to find no of times a elements in list python
thislist = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thislist.count(5)
print(x)
Example 3: python how to count all elements in a list
List = ["Elephant", "Snake", "Penguin"]
print(len(List))
Example 4: python how to count items in array
myArray = [1, 2, 3]
len(myArray)
3
Example 5: how to count things in a list python
list.count(element)
list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green']
color_count = list1.count('green')
print('The count of color: green is ', color_count)
Example 6: count item in list python
list.count(element)