EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
PYTHON Course Bundle - 81 Courses in 1 | 59 Mock Tests
 257+ Hours of HD Videos
81 Courses
59 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.8
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

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

*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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more