Python - Frequency of Elements from Other List

Finding the frequency of elements from one list based on another list is a common data analysis task in Python. This involves counting how many times each element from a reference list appears in a data list.

For example, given these two lists:

reference_list = [1, 2, 3, 4]
data_list = [1, 1, 2, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4]
# Result: {1: 3, 2: 4, 3: 1, 4: 5}

Using Counter()

The Counter class from the collections module efficiently counts occurrences of all elements ?

from collections import Counter

# Create the lists
reference_list = [40, 60, 80, 90]
data_list = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]

# Count frequencies in data_list
counter = Counter(data_list)

# Get frequencies for reference_list elements
result = {element: counter[element] for element in reference_list}

print("The resulting frequency of key elements:")
print(result)
The resulting frequency of key elements:
{40: 2, 60: 2, 80: 2, 90: 1}

Using Loop and Dictionary

A simple approach using the count() method to count occurrences ?

reference_list = [40, 60, 80, 90]
data_list = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]

# Create empty dictionary to store results
result = {}

# Count frequency for each element in reference_list
for element in reference_list:
    result[element] = data_list.count(element)

print("The resulting frequency of key elements:")
print(result)
The resulting frequency of key elements:
{40: 2, 60: 2, 80: 2, 90: 1}

Using defaultdict()

The defaultdict automatically creates missing keys with default values, making frequency counting cleaner ?

from collections import defaultdict

# Create the lists
reference_list = [40, 60, 80, 90]
data_list = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]

# Count frequencies using defaultdict
frequency_count = defaultdict(int)
for element in data_list:
    frequency_count[element] += 1

# Extract frequencies for reference_list elements
result = {element: frequency_count[element] for element in reference_list}

print("The resulting frequency of key elements:")
print(result)
The resulting frequency of key elements:
{40: 2, 60: 2, 80: 2, 90: 1}

Using NumPy unique()

NumPy's unique() function returns unique elements and their counts efficiently ?

import numpy as np

# Create the lists
reference_list = [40, 60, 80, 90]
data_list = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]

# Get unique elements and their counts
unique_elements, counts = np.unique(data_list, return_counts=True)

# Create frequency mapping for reference_list elements
result = {}
for element in reference_list:
    index = np.where(unique_elements == element)[0]
    result[element] = counts[index[0]] if len(index) > 0 else 0

print("The resulting frequency of key elements:")
print(result)
The resulting frequency of key elements:
{40: 2, 60: 2, 80: 2, 90: 1}

Comparison

Method Time Complexity Best For
Counter() O(n) Most readable and efficient
count() O(n×m) Simple, small datasets
defaultdict() O(n) Custom counting logic
np.unique() O(n log n) Numerical data, NumPy arrays

Conclusion

Use Counter() for the most efficient and readable solution. The count() method works for simple cases but becomes slow with large datasets. Choose defaultdict() when you need custom counting logic.

Updated on: 2026-03-27T12:40:27+05:30

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements