Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Count occurrences of an element in a list in Python
Next article icon

Count occurrences of an element in a list in Python

Last Updated : 21 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A common task when working with lists is to count how many times a specific element appears. In Python, we have several ways to count occurrences of an element using both built-in and custom methods.

The simplest and most straightforward way to count occurrences of an element in a list is by using the count() method, which is a built-in method specifically designed for this task.

Using count()

In this example, we have a list and we are counting the occurrences of 2 and 3 using the count() method.

Python
a = [1, 3, 2, 6, 3, 2, 8, 2, 9, 2, 7, 3]  # Count occurrences of 2 print(a.count(2))  # Count occurrences of 3 print(a.count(3)) 

Output
4 3 

Below are the other methods by which we can use to count the occurrences of an element in a list.

Table of Content

  • Using a Loop
  • Using countOf()
  • Using Counter from collections

Using a Loop

In this method, iterate over the list using loop (for loop) and keep a counter variable to count the occurrences. Each time we find the target element, increase the counter by one.

Python
a = [1, 3, 2, 6, 3, 2, 8, 2, 9, 2, 7, 3]  # Initial count is zero count = 0  # Iterate over the list for val in a:      	# If num is equal to 3  	if val == 3:                # Increase the counter 		count += 1  print(count) 

Output
3 

Using countOf()

The countOf() function is equivalent to using the count() method of a list, but it comes from the operator module.

It takes two arguments: the sequence in which we want to count and the value that we want to count. Let's look at the syntax given below:

import operator

operator.countOf(sequence, value)

Using Counter from collections

The Counter class from the collections module can count occurrences for all elements and returns the results as a dictionary. Let's see how can we use this to count occurrence of a single element.

Python
from collections import Counter  a = [1, 3, 2, 6, 3, 2, 8, 2, 9, 2, 7, 3]  # Create a counter objects res = Counter(a)  # Get count of 3 print(res[3]) 

Output
3 

Note: This method is not efficient for finding occurrence of single element because it requires O(n) extra space to create a new dictionary. But this method is very efficient when finding all occurrences of elements.

Related Articles:

  • Python String count() Method
  • Count of elements matching particular condition in Python
  • Count Occurrences of Specific Value in Pandas Column

Next Article
Count occurrences of an element in a list in Python

C

chinmoy lenka
Improve
Article Tags :
  • Python
  • python-list
  • Python list-programs
Practice Tags :
  • python
  • python-list

Similar Reads

    Python | Count occurrences of an element in a Tuple
    In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple:
    3 min read
    Count the Number of Null Elements in a List in Python
    In data analysis and data processing, It's important to know about Counting the Number of Null Elements. In this article, we'll explore how to count null elements in a list in Python, along with three simple examples to illustrate the concept. Count the Number of Null Elements in a List in PythonIn
    3 min read
    Python - Find all elements count in list
    In Python, counting the occurrences of all elements in a list is to determine how many times each unique element appears in the list. In this article, we will explore different methods to achieve this. The collections.Counter class is specifically designed for counting hashable objects. It provides
    3 min read
    Count occurrences of a character in string in Python
    We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three
    2 min read
    Count number of lines in a text file in Python
    Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output:
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences