English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The count() method returns the occurrence of a certain element in the list.
In simple terms, the count() method calculates the number of times an element appears inin the listIt returns the number of occurrences and returns it.
The syntax of count() method is:
list.count(element)
The count() method takes one parameter:
element-To find the element to be counted.
The count() method returns the occurrence of a certain element in the list.
# Vowel list vowels = ['a', 'e', 'i', 'o', 'i', 'u'] # Calculate element 'i' count = vowels.count('i') # Print count print('i occurrence is:', count) # Calculate element 'p' count = vowels.count('p') # Print count print('p occurrence is:', count)
The output when running the program is:
i occurrence is: 2 p occurrence is: 0
# Random list random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]] # Calculate ('a', 'b') count = random.count(('a', 'b')) # Print count print("Calculate ('a', 'b') occurrence is:", count) # Calculate [3, 4] count = random.count([3, 4] # Print count print("Calculate [3, 4] The occurrence is: "count"
The output when running the program is:
Calculate ['a', 'b'] occurrence is: 2 Calculate [3, 4The occurrence is: 1