0
0
PythonProgramBeginner · 2 min read

Python Program to Find Frequency of Elements in List

You can find the frequency of elements in a list using a dictionary with collections.Counter(your_list) or by looping through the list and counting each element manually.
📋

Examples

Input[1, 2, 2, 3, 3, 3]
Output{1: 1, 2: 2, 3: 3}
Input['apple', 'banana', 'apple', 'orange', 'banana', 'banana']
Output{'apple': 2, 'banana': 3, 'orange': 1}
Input[]
Output{}
🧠

How to Think About It

To find how many times each element appears in a list, think of creating a record for each unique item and increasing its count every time you see it again. This is like counting how many times each type of fruit appears in a basket by checking one by one and keeping track.
📐

Algorithm

1
Get the input list.
2
Create an empty dictionary to store frequencies.
3
For each element in the list, check if it is already in the dictionary.
4
If it is, increase its count by one; if not, add it with count one.
5
Return or print the dictionary with element counts.
💻

Code

python
from collections import Counter

my_list = [1, 2, 2, 3, 3, 3]
frequency = Counter(my_list)
print(dict(frequency))
Output
{1: 1, 2: 2, 3: 3}
🔍

Dry Run

Let's trace the list [1, 2, 2, 3, 3, 3] through the code using Counter.

1

Input list

my_list = [1, 2, 2, 3, 3, 3]

2

Count elements

Counter counts each element: 1 once, 2 twice, 3 three times

3

Convert to dictionary

frequency = {1: 1, 2: 2, 3: 3}

4

Print result

Output: {1: 1, 2: 2, 3: 3}

ElementCount
11
22
33
💡

Why This Works

Step 1: Using Counter

The Counter class automatically counts how many times each element appears in the list.

Step 2: Storing counts

It stores these counts in a dictionary-like object where keys are elements and values are counts.

Step 3: Printing result

Converting to a regular dictionary and printing shows the frequency of each element clearly.

🔄

Alternative Approaches

Manual counting with dictionary
python
my_list = [1, 2, 2, 3, 3, 3]
frequency = {}
for item in my_list:
    if item in frequency:
        frequency[item] += 1
    else:
        frequency[item] = 1
print(frequency)
This method does not require importing modules and works well for beginners but is longer to write.
Using dictionary comprehension with count()
python
my_list = [1, 2, 2, 3, 3, 3]
frequency = {item: my_list.count(item) for item in set(my_list)}
print(frequency)
This is simple but inefficient for large lists because it counts each element multiple times.

Complexity: O(n) time, O(n) space

Time Complexity

Counting frequencies requires checking each element once, so it takes linear time relative to the list size.

Space Complexity

A dictionary stores counts for unique elements, so space depends on the number of unique items.

Which Approach is Fastest?

Using Counter is fastest and most efficient; manual counting is slightly slower but clear; using count() in comprehension is slowest for large lists.

ApproachTimeSpaceBest For
collections.CounterO(n)O(n)Fast and easy for all sizes
Manual dictionary countingO(n)O(n)Clear logic without imports
Dictionary comprehension with count()O(n²)O(n)Small lists or simple scripts
💡
Use collections.Counter for the simplest and fastest way to count frequencies in Python.
⚠️
Beginners often try to count frequencies without checking if the element is already counted, causing errors or wrong counts.