0
0
PythonProgramBeginner · 2 min read

Python Program to Create Frequency Table from List

You can create a frequency table from a list in Python using a dictionary comprehension or the collections.Counter class, for example: from collections import Counter; freq = Counter(your_list).
📋

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 create a frequency table, you look at each item in the list and count how many times it appears. You keep track of these counts in a dictionary where the keys are the items and the values are their counts. This way, you can quickly see how many times each item occurs.
📐

Algorithm

1
Start with an empty dictionary to hold counts.
2
Go through each item in the list one by one.
3
If the item is not in the dictionary, add it with count 1.
4
If the item is already in the dictionary, increase its count by 1.
5
After checking all items, return the dictionary with counts.
💻

Code

python
from collections import Counter

items = [1, 2, 2, 3, 3, 3]
frequency_table = Counter(items)
print(dict(frequency_table))
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

Initialize list

items = [1, 2, 2, 3, 3, 3]

2

Create frequency table

frequency_table = Counter(items) counts each item: 1->1, 2->2, 3->3

3

Print result

print(dict(frequency_table)) outputs {1: 1, 2: 2, 3: 3}

ItemCount
11
22
33
💡

Why This Works

Step 1: Using Counter

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

Step 2: Dictionary output

Converting the Counter object to a dictionary shows the frequency table with items as keys and counts as values.

🔄

Alternative Approaches

Manual dictionary counting
python
items = [1, 2, 2, 3, 3, 3]
frequency_table = {}
for item in items:
    if item in frequency_table:
        frequency_table[item] += 1
    else:
        frequency_table[item] = 1
print(frequency_table)
This method does not require imports and shows the counting process explicitly but is longer.
Dictionary comprehension with count method
python
items = [1, 2, 2, 3, 3, 3]
frequency_table = {item: items.count(item) for item in set(items)}
print(frequency_table)
This is concise but inefficient for large lists because it counts each item multiple times.

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

Time Complexity

Counting frequencies requires checking each item once, so it takes O(n) time where n is the list length.

Space Complexity

The dictionary stores counts for each unique item, so space is O(k) where k is the number of unique items.

Which Approach is Fastest?

Using Counter is fastest and most readable. Manual counting is similar but more verbose. Dictionary comprehension with count() is slower for large lists.

ApproachTimeSpaceBest For
collections.CounterO(n)O(k)Large lists, simplicity
Manual dictionary countingO(n)O(k)No imports, learning purpose
Dictionary comprehension with count()O(n*k)O(k)Small lists, concise code
💡
Use collections.Counter for a simple and efficient frequency table in Python.
⚠️
Beginners often forget to handle items not yet in the dictionary, causing errors when incrementing counts.