Python List count() method [With Examples]

In this Python article, we will focus on the Python List count() method, which is used to count the occurrences of a specific element in a list.

List count() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List count() method
  • Syntax of the count() method
  • Purpose and use cases of the count() method

Python List count() method

The count() method is a built-in method in Python that is used to count the number of occurrences of a specified element within a list. The method returns an integer representing the total number of times the given element appears in the Python list.

The syntax for the count() method is as follows:

list_name.count(element)

Here, list_name is the name of the list and element is the item whose occurrences we want to count.

The return value is as follows:

The count() method returns an integer value representing the number of times the specified element occurs in the Python list.

count() method in Python List Examples

Let’s look at some examples to better understand the Python count() method.

Example#1 Basic Usage

states = ['California', 'Texas', 'New York', 'California', 'Florida', 'Texas']
california_count = states.count('California')
print(california_count)

In this example, we have a Python list of US states, and we are using the count() method to find the number of times ‘California’ occurs in the list. The output is 2, as there are two ‘California’ elements in the Python list.

READ:  While loop with multiple conditions in Python

Output:

Python List count method
Python List count method

Example#2 Counting Non-Existent Elements

states = ['California', 'Texas', 'New York', 'California', 'Florida', 'Texas']
hawaii_count = states.count('Hawaii')
print(hawaii_count) 

In this example, we try to count the occurrences of ‘Hawaii’ in the Python list, but since it is not present, the count() method returns 0.

Output:

Python List count method example
Python List count method example

Example#3 Counting Elements in a List of Lists

nested_list = [['California', 'Los Angeles'], ['California', 'San Francisco'], ['New York', 'New York'], ['California', 'San Diego']]
sub_list_count = nested_list.count(['California', 'San Francisco'])
print(sub_list_count)

Here, we have a nested Python list and we want to count the occurrences of the sublist [‘California’, ‘San Francisco’]. The count() method returns 1, as the sublist [‘California’, ‘San Francisco’] occurs only once in the nested list.

Output:

List count method in Python
List count method in Python

Example#4 Counting integers in a list

numbers = [1, 2, 3, 2, 4, 2, 5, 6, 7, 2]
two_count = numbers.count(2)
print(two_count)

In this example, we have a Python list of integers, and we are using the count() method to find the number of times the number ‘2’ occurs in the list. The output is 4, as there are four occurrences of the number ‘2’ in the list.

Output:

List count method in Python example
List count method in Python example

Example#5 Counting occurrences of a tuple in a list

city_tuples = [('California', 'Los Angeles'), ('Texas', 'Houston'), ('California', 'Los Angeles'), ('New York', 'New York'), ('California', 'Los Angeles'), ('Florida', 'Miami')]
tuple_count = city_tuples.count(('California', 'Los Angeles'))
print(tuple_count)

In this example, we have a Python list of tuples representing states and their cities in the United States. We want to count the occurrences of the tuple (‘California’, ‘Los Angeles’). The Python count() method returns 3, as the tuple (‘California’, ‘Los Angeles’) occurs three times in the list.

Output:

count method in Python List
count method in Python List

Read: How to count occurrences of a character in a Python list

READ:  How to Print in the Same Line in Python [6 Methods]

Example#6 Counting the occurrences of a specific character in a string

sentence = "The quick brown fox jumps over the lazy dog"
char_count = sentence.count('o')
print(char_count)

In this example, we have a Python string, and we want to count the occurrences of the character ‘o’. The count() method returns 4, as the character ‘o’ occurs four times in the sentence.

Output:

count method in Python List example
count method in Python List example

Example#7 Counting occurrences of elements in a list of mixed types

mixed_list = ['California', 'Texas', 'New York', 1776, 'California', 'Florida', 1776, 'Texas', 1776]
integer_count = mixed_list.count(1776)
print(integer_count)

In this example, we have a Python list containing elements of different types (strings representing US states and an integer representing a significant year in US history). We use the count() method to find the number of times the integer ‘1776’ occurs in the list.

The output is 3, as there are three occurrences of the integer ‘1776’ in the list.

Output:

Introduction to Python List count method
Python List count method

Conclusion

The Python List count() method is a simple yet powerful tool for working with lists in Python. It is particularly useful when you need to find the frequency of elements in a list for data analysis or manipulation tasks.

By understanding the count() method and its usage, you can improve your efficiency when working with lists in Python.

You may also like to read the following articles: