my_list=['a','b','c','b','a','d']
print("Number of times a appears : ", my_list.count('a'))
print("Number of times b appears : ", my_list.count('b'))
print("Number of times c appears : ", my_list.count('c'))
Output
Number of times a appears : 2
Number of times b appears : 2
Number of times c appears : 1
my_list = ['x', 'y', 'z']
print("Count of 'a':", my_list.count('a')) # should print 0, because 'a' is not in list
class Person:
def __init__(self, name):
self.name = name
def __eq__(self, other):
return isinstance(other, Person) and self.name == other.name
p1 = Person("Alice")
p2 = Person("Bob")
p3 = Person("Alice")
lst = [p1, p2, p3]
print("Count of Person('Alice'):", lst.count(Person("Alice"))) # 2
from collections import Counter
my_list = ['a', 'b', 'b', 'c', 'a', 'd', 'b']
cnt = Counter(my_list)
print("Count of 'a':", cnt['a'])
print("Count of 'b':", cnt['b'])
print("Top two frequent items:", cnt.most_common(2))
text = "apple orange apple banana apple fruit"
words = text.split()
print("Number of 'apple':", words.count('apple'))
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.