Python List count() method with Examples

In the previous article, we have discussed Python Tuple index() method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

 List count() method in Python:

The count() function returns the number of occurrences of the particular element in the given list.

Syntax:

givenlist.count(gvnelement)

Parameters:

The count() method only accepts one argument.

  • gvnelement: The element whose frequency is to be calculated

Return Value:

The count() method returns the number of occurrences of the given element in the given List.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']
Given Element =  "python"

Output:

The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :
2

Example2:

Input:

Given List =  [7, 1, 8, 2, 8, 1, 9, 8, 4]
Given Element = 8

Output:

Enter some random list elements = 7 1 8 2 8 1 9 8 4
Enter some random element = 8
The count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :
3

List count() method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

Example-1:

String List:

  • Give the string List as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the string List as static input and store it in a variable.
gvnlst = ['hello', 'this', 'is', 'python', 'is', 'programs', 'python']
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = "python"
# Pass the given element above as the argument to the count function
# for the given List and store the result in a variable say resltcnt
# (It has the count of the given element in the given List).
resltcnt = gvnlst.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given List', gvnlst, 'is :')
print(resltcnt)

Output:

The count of the given element { python } in the given List ['hello', 'this', 'is', 'python', 'is', 'programs', 'python'] is :
2

Example-2:

Integer List:

  • Give the Integer List as static input and store it in a variable.
  • Give the element whose frequency is to be calculated as static input and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List ).
  • Print the value of resltcnt.
  • The Exit of the Program.

Below is the implementation:

# Give the Integer  List as static input and store it in a variable.
gvnlst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5]
# Give the element whose frequency is to be calculated as static input
# and store it in another variable.
gvnelemeent = 3
# Pass the given element above as the argument to the count function
# for the given List and store the result in a variable say resltcnt
# (It has the count of the given element in the given List).
resltcnt = gvnlst.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given List', gvnlst, 'is :')
print(resltcnt)

Output:

The count of the given element { 3 } in the given List [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 11, 2, 3, 45, 6, 2, 3, 5] is :
4

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the Integer List as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the element as user input using the int(), input() functions, and store it in another variable.
  • Pass the given element above as the argument to the count function for the given List and store the result in a variable say resltcnt(It has the count of the given element in the given List).
  • Print the value of resltcnt.
  • The Exit of Program.

Below is the implementation:

# Give the Integer List as user input using list(),map(),input(),and split() functions.
gvnlistt = list(map(int,input('Enter some random list elements = ').split()))
# Give the element as user input using the int(), input() functions,
# and store it in another variable.
gvnelemeent = int(input('Enter some random element = '))
# Pass the given element above as the argument to the count function
# for the given list and store the result in a variable say resltcnt
# (It has the count of the given element in the given list).
resltcnt = gvnlistt.count(gvnelemeent)
# Print the value of resltcnt.
print('The count of the given element {', gvnelemeent,
      '} in the given list', gvnlistt, 'is :')
print(resltcnt)

Output:

Enter some random list elements = 7 1 8 2 8 1 9 8 4
Enter some random element = 8
The count of the given element { 8 } in the given list [7, 1, 8, 2, 8, 1, 9, 8, 4] is :
3

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.