How to Count the Number of Occurrences of a Value in a List in Python?

Estimated read time 1 min read

In Python, you can count the number of occurrences of a value in a list by using the built-in list method count(). Here’s an example:

# Define the input list
input_list = [3, 7, 2, 3, 8, 3, 1, 3, 5, 3]

# Define the value to count
value_to_count = 3

# Call the count() method on the input list
value_count = input_list.count(value_to_count)

# Print the result
print("The value {} appears {} times in the list.".format(value_to_count, value_count))


The value 3 appears 5 times in the list.

In this example, the count() method is called on the input_list and takes the value_to_count as an argument. It returns the number of occurrences of the value in the list, which is stored in the value_count variable. Finally, the result is printed using a formatted string to display the value and its count.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply