What is count in python list?

In this tutorial, we will learn about the Python List count() method with the help of examples.

The count() method returns the number of times the specified element appears in the list.

Example

# create a list
numbers = [2, 3, 5, 2, 11, 2, 7]

# check the count of 2 count = numbers.count(2)

print('Count of 2:', count) # Output: Count of 2: 3


Syntax of List count()

The syntax of the count() method is:

list.count(element)

count() Parameters

The count() method takes a single argument:

  • element - the element to be counted

Return value from count()

The count() method returns the number of times element appears in the list.


Example 1: Use of count()

# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# count element 'i' count = vowels.count('i')

# print count print('The count of i is:', count)

# count element 'p' count = vowels.count('p')

# print count print('The count of p is:', count)

Output

The count of i is: 2
The count of p is: 0

Example 2: Count Tuple and List Elements Inside List

# random list
random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]]

# count element ('a', 'b') count = random.count(('a', 'b'))

# print count print("The count of ('a', 'b') is:", count)

# count element [3, 4] count = random.count([3, 4])

# print count print("The count of [3, 4] is:", count)

Output

The count of ('a', 'b') is: 2
The count of [3, 4] is: 1

The count() is a built-in function in Python. It will return the total count of a given element in a list. The count() function is used to count elements on a list as well as a string.

In this Python tutorial, you will learn:

  • Python count
  • Python List count()
  • Example 1: List Count
  • Example 2: Find the count of elements (Duplicates) in a givenlist

Python List count()

The count() is a built-in function in Python. It will return you the count of a given element in the list.

Syntax:

list.count(element)

Parameters:

element: The element you want to find the count.

ReturnValue:

The count() method will return an integer value, i.e., the count of the given element from the given list. It returns a 0 if the value is not found in the given list.

Example 1: List Count

Following example shows the working of count() function on a list:

list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green']
color_count = list1.count('green')
print('The count of color: green is ', color_count)

Output:

The count of color: green is  3

Example 2: Find the count of elements (Duplicates) in a givenlist

list1 = [2,3,4,3,10,3,5,6,3]
elm_count = list1.count(3)
print('The count of element: 3 is ', elm_count)

Output:

The count of element: 3 is  4

Summary:

  • The count() is a built-in function in Python. It will return you the count of a given element in a list or a string.
  • In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element.
  • The count() method returns an integer value.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python List count() method returns the count of how many times a given object occurs in a List.

    Python List count() method Syntax

    Syntax:  list_name.count(object) 

    Parameters: 

    • object: is the item whose count is to be returned.

    Returns:  Returns the count of how many times object occurs in the list.

    Exception: 

    • TypeError: Raises TypeError If more than 1 parameter is passed in count() method.

    Python List count() method Example

    Python3

    list2 = ['a', 'a', 'a', 'b', 'b', 'a', 'c', 'b']

    print(list2.count('b'))

    Output:

    3

    Example 1: Count Tuple and List Elements Inside List

    Count occurrences of List and Tuples inside a list using count() method.

    Python3

    list1 = [ ('Cat', 'Bat'), ('Sat', 'Cat'), ('Cat', 'Bat'),

              ('Cat', 'Bat', 'Sat'), [1, 2], [1, 2, 3], [1, 2] ]

    print(list1.count(('Cat', 'Bat')))

    print(list1.count([1, 2]))

    Output: 

    2
    2

    Exceptions while using Python list count() method

    TypeError

    Python list count() method raises TypeError when more than 1 parameter is passed.

    Python3

    list1 = [1, 1, 1, 2, 3, 2, 1]

    print(list1.count(1, 2))

    Output:

    Traceback (most recent call last):
      File "/home/41d2d7646b4b549b399b0dfe29e38c53.py", line 7, in 
        print(list1.count(1, 2))  
    TypeError: count() takes exactly one argument (2 given)

    Practical Application

     Let’s say we want to count each element in a list and store it in another list or say dictionary.

    Python3

    lst = ['Cat', 'Bat', 'Sat', 'Cat', 'Mat', 'Cat', 'Sat']

    print ([ [l, lst.count(l)] for l in set(lst)])

    print (dict( (l, lst.count(l) ) for l in set(lst)))

    Output:

    [['Mat', 1], ['Cat', 3], ['Sat', 2], ['Bat', 1]]
    {'Bat': 1, 'Cat': 3, 'Sat': 2, 'Mat': 1}

    What is count function in list?

    The COUNT function counts the number of cells that contain numbers, and counts numbers within the list of arguments. Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers.

    What is the syntax for count in Python?

    The syntax of count() method is: string. count(substring, start=..., end=...)

    What do you mean by list count OBJ?

    obj − This is the object to be counted in the list.

    What is count in tuple?

    Python Tuple count() Method The count() method returns the number of times a specified value appears in the tuple.