EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Python Lists Tutorial Python List count
 

Python List count

Priya Pedamkar
Article byPriya Pedamkar

Updated March 28, 2023

Python List count

 

 

Introduction to Python List count

The following article provides an outline for Python List count. Count() is an in-built function in Python that returns the counts or, in many simple terms, how many times a given object occurs in the list. In other words, it returns the frequency of occurrence of that object within the list we are searching for. If we are looking forward to a particular column’s mode in a specified Dataset, then the count function can be utilized to take care of the same by implementing this column as a list.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

The syntax of the Python count() function to return the frequency of an object is as below:

list.count(Object)
  • The object is the numeral, string or any other whose count is to be returned.
  • Count() method returns the count of how many times the object occurs in the list.

How does Python List count Work?

  • Here we started by creating a list of integers with each element having a single of multiple occurrences.
  • Count() function is used as an attribute to the list1, along with an argument.
  • Here the argument passed is an element whose occurrence we need to check within the list.
  • The returned value is the integer which represents the counts of the element passed as an argument to the function.

Examples of Python List count

Given below are the examples mentioned:

Example #1

Code:

## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 ,5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times the numeral 6 occours in the list1
print(list1.count(6))

Output:

Python List count Example 1

What happens if we need to check the occurrences of two elements in a single go. Do we pass two elements separated by a comma to the count() function?

Let’s check this out by taking another example on the same.

Example #2

Code:

## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 , 5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count(6,1))

Output:

Integers List Example 2

This program throws an error when passed two elements to check the occurrences of the same in the list. So how can we check the occurrences of multiple elements in the same list?

Yes, we need to call the count() function on the same list again, bypassing the other elements.

Let’s check this out by taking another example on the same.

Example #3

Code:

## Python program to count the number of times an object occurs in the list of integers
list1 = [ 1 , 1 , 1 , 2 , 3 , 2 , 1 , 23 , 43 , 54 , 4 , 5 ,65 , 67 , 76 , 54 , 34 , 2 , 4 , 5 , 6 , 6 , 6, 7 , 8 , 9]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count(6))
print(list1.count(1))

Output:

Python List count Example 1

How does the count function behave with the other data types like char or strings etc.?

Let’s explore the same by taking some examples.

Example #4

Code:

## Python program to count the number of times an object occurs in the list of chars
list1 = [ 'e' , 'r' , 't' , 'e' , 'r' , 't' ,  'e' , 'r' , 't' ,  'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' ,'e' , 'r' , 't' ,'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't' , 'e' , 'r' , 't'  ]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count('e'))
print(list1.count('z'))

Output:

Chars List Example 4

Notice here in the second print, where we have passed ‘z’ as an element to check its occurrence in the list.

We get an output of 0 for this iteration. The reason being ‘z’ is not even a part of the list. Let’s explore the same by taking some more examples of how it works for strings.

Example #5

Code:

## Python program to count the number of times an object occurs in the list of string
list1 = [ 'edu' , 'cba' ,  'learn' , 'ca' , 'na'  ]
# Count() function as an attribute to count the number of times two elements occours in the list1
print(list1.count('edu'))

Output:

String List Example 5

Conclusion

Pythons List count() function returns the number of times the element is present in the list. We cannot pass multiple elements in the list to check for their occurrences. If we need to do the same, we need to pass the elements separate in separate count functions.

Recommended Articles

This is a guide to Python List count. Here we discuss a brief overview of Python List count and its examples, along with its code implementation. You can also go through our other suggested articles to learn more –

  1. Python Range Function
  2. Python Set Function
  3. Python Turtle
  4. Python Reverse List
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW