Finding frequency in list of tuples in Python

When working with lists containing tuples, you may need to find how frequently a specific element appears across all tuples. Python provides several efficient methods to count occurrences of elements within tuple structures.

Using count() and map()

The map() function extracts elements from each tuple, then count() finds the frequency of a specific element ?

# initializing list of tuples
fruits_days = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]

# Given list
print("Given list of tuples:", fruits_days)

# Frequency in list of tuples
freq_result = list(map(lambda i: i[0], fruits_days)).count('Apple')

# printing result
print("The frequency of 'Apple' is:", freq_result)
Given list of tuples: [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]
The frequency of 'Apple' is: 3

Using Counter from collections

The Counter class provides a more elegant solution by counting all elements at once and allowing easy lookup ?

from collections import Counter

# initializing list of tuples
fruits_days = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]

# Given list
print("Given list of tuples:", fruits_days)

# Frequency in list of tuples using Counter
counter = Counter(i[0] for i in fruits_days)
freq_result = counter['Apple']

# printing result
print("The frequency of 'Apple' is:", freq_result)

# Show all frequencies
print("All fruit frequencies:", dict(counter))
Given list of tuples: [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]
The frequency of 'Apple' is: 3
All fruit frequencies: {'Apple': 3, 'Banana': 1, 'Orange': 1}

Using Dictionary Comprehension

You can create a frequency dictionary for all elements in one line using dictionary comprehension ?

# initializing list of tuples
fruits_days = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]

# Create frequency dictionary
frequency_dict = {}
for item, _ in fruits_days:
    frequency_dict[item] = frequency_dict.get(item, 0) + 1

print("Given list of tuples:", fruits_days)
print("Frequency dictionary:", frequency_dict)
print("The frequency of 'Apple' is:", frequency_dict.get('Apple', 0))
Given list of tuples: [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', 'Wed'), ('Orange', 'Thu'), ('Apple', 'Fri')]
Frequency dictionary: {'Apple': 3, 'Banana': 1, 'Orange': 1}
The frequency of 'Apple' is: 3

Comparison

Method Best For Performance Memory Usage
map() + count() Single element lookup O(n) per lookup Low
Counter Multiple lookups O(n) once, O(1) lookups Medium
Dictionary Custom frequency logic O(n) Medium

Conclusion

Use Counter for multiple frequency lookups as it's most efficient. For single element frequency, map() with count() provides a simple solution. Dictionary comprehension offers the most flexibility for custom counting logic.

Updated on: 2026-03-15T17:49:34+05:30

598 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements