A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-list-count-method/ below:

Python List count() method - GeeksforGeeks

Python List count() method

Last Updated : 27 Apr, 2025

The count() method is used to find the number of times a specific element occurs in a list. It is very useful in scenarios where we need to perform frequency analysis on the data.

Let's look at a basic example of the count() method.

Python
a = [1, 2, 3, 1, 2, 1, 4]

c = a.count(1)
print(c)

Explanation: a.count(1) counts the occurrences of 1 in the list, as it is occurring 3 times in the list so the output is 3.

Syntax

list_name.count(value)

Prameters:

Return Type: it an integer value, which represents the number of times the specified element appears in the list.

Examples of count() method Example 1: Lits Containing Different Datatypes

The count() method also works well with list that have different types of data.

Python
a = [1, 'GfG', 3.14, 'GfG', 1, True]

c1 = a.count('GfG')
c2 = a.count(1)

print(c1)
print(c2)

Explanation:

Example 2: Count occurrence of sub-list in list of Lists

The count() method does not search within nested lists and it will only count occurrences at the top level.

Python
a = [1, [2, 3], 1, [2, 3], 1]

c = a.count([2, 3])
print(c)

Explanation: The sublist [2, 3] is treated as a single element in the list. The count() method counts it twice because it appears twice as a nested list.

Example 3: Counting Word Frequency in a List

This example simulates word analysis in a sentence by counting how often a word appears.

Python
s = "python is easy to learn and python is powerful".split()

c1 = s.count("python")
c2 = s.count("is")

print(c1)
print(c2)

Output
count of python:  2
count of is:  2

Explanation:

Related articles:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4