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:
Counting the Frequencies in a List Using Dictionary in Python
Next article icon

Counting the Frequencies in a List Using Dictionary in Python

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Counting the frequencies of items in a list using a dictionary in Python can be done in several ways. For beginners, manually using a basic dictionary is a good starting point.

Using Manual Method

It uses a basic dictionary to store the counts, and the logic checks whether the item is already in the dictionary. If it is, the count is incremented; if it's not, the item is added to the dictionary with an initial count of 1.

Python
a = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']  # Create an empty dictionary to store the counts b = {}  # Loop through the list for c in a:     # If the item is already in dictionary, increase its count     if c in b:         b[c] += 1     # If the item is not in dictionary, add it with a count of 1     else:         b[c] = 1 print(b) 

Output
{'apple': 2, 'banana': 3, 'orange': 1} 

Let's explore more methods to count the frequencies in a List Using Dictionory in Python.

Table of Content

  • Using get() Method
  • Using a Default Dictionary (defaultdict)
  • Using Manual Method
  • Using List Comprehension

Using a Default Dictionary (defaultdict)

The defaultdict from the collections module automatically initializes new keys with a default value (in this case, 0), so there's no need to explicitly check if the key exists before incrementing the count.

Python
from collections import defaultdict  # List of items a = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']  # Create a defaultdict with default value 0 b = defaultdict(int)  # Loop through the list for c in a:     b[c] += 1  # Print the frequency of each item print(dict(b)) 

Output
{'apple': 2, 'banana': 3, 'orange': 1} 

Using get() Method

This will count the frequency of each item in the list using the get() method. The get() method is used to return the current count of an item or 0 if the item is not yet in the dictionary.

Python
a = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']  # Create an empty dictionary to store the counts b = {}  # Loop through the list for c in a:     # Use get() to either return the current count or 0      #if the item is not in the dictionary     b[c] = b.get(c, 0) + 1 print(b) 

Output
{'apple': 2, 'banana': 3, 'orange': 1} 

Using List Comprehension

For small lists, we can also use a list comprehension along with the count() method to get the frequency of each item. But this method is not as efficient as the others since count() will loop through the entire list each time.

Python
a = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']  # Create a list of unique items b = list(set(a))  # Use list comprehension to count the frequency of each unique item c = {item: a.count(item) for item in b}  # Print the frequency of each item print(c) 

Output
{'banana': 3, 'apple': 2, 'orange': 1} 

Next Article
Counting the Frequencies in a List Using Dictionary in Python

S

SouravAChowdhury_97
Improve
Article Tags :
  • Python
  • Python Programs
  • frequency-counting
  • python-list
  • python-dict
  • Python dictionary-programs
Practice Tags :
  • python
  • python-dict
  • python-list

Similar Reads

    Python - Convert Frequency dictionary to list
    When we convert a frequency dictionary to a list, we are transforming the dictionary into a list of key-value or just the keys/values, depending on needs. We can convert a frequency dictionary to a list using methods such as list comprehension, loops, extend() method and itertools.chain() function.F
    3 min read
    Python - Associated Values Frequencies in Dictionary
    Sometimes, while working with dictionaries, we can have problem in which we need to compute the values associated to each value in dictionary in records list. This kind of problem is peculiar, but can have application in development domains. Lets discuss certain way in which this task can be perform
    5 min read
    Count the Key from Nested Dictionary in Python
    In Python, counting the occurrences of keys within a nested dictionary often requires traversing through its complex structure. In this article, we will see how to count the key from the nested dictionary in Python. Count the Key from the Nested Dictionary in PythonBelow are some ways and examples b
    4 min read
    Count all Elements in a Nested Python Dictionary
    Nested dictionaries are a common data structure in Python, often used to represent complex relationships and hierarchies. When working with nested dictionaries, you may encounter situations where you need to count all the elements within them. In this article, we will explore some simple and commonl
    3 min read
    Python - Values frequency across Dictionaries lists
    Given two list of dictionaries, compute frequency corresponding to each value in dictionary 1 to second. Input : test_list1 = [{"Gfg" : 6}, {"best" : 10}], test_list2 = [{"a" : 6}, {"b" : 10}, {"d" : 6}}] Output : {'Gfg': 2, 'best': 1} Explanation : 6 has 2 occurrence in 2nd list, 10 has 1. Input :
    6 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