The count() method returns the number of times a specified item appears in the list. Here's a quick example:
models = ['Claude', 'ChatGPT', 'Gemini', 'ChatGPT']
count = models.count('Gemini')
print(f'Count of Gemini: {count}')
count = models.count('ChatGPT')
print(f'Count of ChatGPT: {count}')
Output
Count of Gemini: 1 Count of ChatGPT: 2
count() Syntax
The syntax of count() is:
result = my_list.count(item)
Arguments
count() takes a single argument, the item whose count is to be found.
Return Value
count() returns an integer (the number of times the item appears).
Example: count() in Nested lists
groups = [[1, 2], [1], [1, 2], [2, 3]]
count = groups.count([1, 2])
print(f"Count of [1, 2] is: {count}")
count = groups.count([1])
print(f"Count of [1] is: {count}")
count = groups.count(1)
print(f"Count of 1 is: {count}")
Output
Count of [1, 2] is: 2 Count of [1] is: 1 Count of 1 is: 0