count elements in list python

38

>>> from collections import Counter
>>> mylist = ["Bob", "Mike", "Bob", "Mike", "Mike", "Mike", "Bob"]
>>> Counter(mylist)
Counter({'Mike': 4, 'Bob': 3})
colors = ['red', 'green', 'blue', 'orange','blue']
color_count = colors.count('blue')
print('The count of color: blue is ', color_count)
# create a list
numbers = [2, 3, 5, 2, 11, 2, 7]

# check the count of 2
count = numbers.count(2)


print('Count of 2:', count)

# Output: Count of 2: 3

Comments

Submit
0 Comments