You can use count () when you want to count how many elements in a list are. It can also be used for strings.
count()
l = [1, 2, 2, 3, 3, 3]
print(l.count(2))
#Output result: 2
s = 'hello world!'
print(s.count('o'))
#Output result: 2
l = [1, 2, 2, 3, 3, 3]
print(len([a for a in l if a == 2])) # 2
l = [1, 2, 2, 3, 3, 3]
print(sum([a == 2 for a in l])) # 2
You can count the elements that meet the conditions here.
Recommended Posts