English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python list count() usage and example

Python list methods

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)

count() parameter

The count() method takes one parameter:

  • element-To find the element to be counted.

count() return value

The count() method returns the occurrence of a certain element in the list.

Example1: Calculate the occurrence of elements 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

Example2: Calculate the occurrence of tuples and list them

# 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

Python list methods