Python – Fractional Frequency of elements in List

When you need to find the fractional frequency of elements in a list, you can use a dictionary to track element occurrences, combined with the Counter method from the collections module. This approach shows how many times each element has appeared out of its total occurrences as you iterate through the list.

Understanding Fractional Frequency

Fractional frequency represents the current occurrence count of an element divided by its total occurrence count. For example, if element 14 appears twice in a list, its fractional frequencies would be "1/2" for the first occurrence and "2/2" for the second occurrence.

Example

Below is a demonstration of finding fractional frequency using Counter and dictionary comprehension −

from collections import Counter

my_list = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65]

print("The list is :")
print(my_list)

my_num = {index : 0 for index in set(my_list)}

my_denominator = Counter(my_list)

my_result = []
for element in my_list:
   my_num[element] += 1
   my_result.append(str(my_num[element]) + '/' + str(my_denominator[element]))

print("The result is :")
print(my_result)
The list is :
[14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65]
The result is :
['1/2', '1/1', '1/1', '1/2', '1/1', '1/1', '1/1', '1/1', '2/2', '2/2', '1/1', '1/1']

How It Works

The solution involves three main steps:

  1. Dictionary initialization: Creates a dictionary with unique elements from the list, each initialized to 0 for tracking current occurrence counts
  2. Counter creation: Uses Counter to determine the total occurrence count for each element
  3. Fractional calculation: Iterates through the list, incrementing the current count and creating fraction strings

Alternative Approach Using defaultdict

Here's a cleaner implementation using defaultdict −

from collections import Counter, defaultdict

numbers = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65]

# Count total occurrences
total_counts = Counter(numbers)

# Track current occurrences
current_counts = defaultdict(int)

fractional_frequencies = []

for num in numbers:
    current_counts[num] += 1
    fraction = f"{current_counts[num]}/{total_counts[num]}"
    fractional_frequencies.append(fraction)

print("Original list:", numbers)
print("Fractional frequencies:", fractional_frequencies)
Original list: [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65]
Fractional frequencies: ['1/2', '1/1', '1/1', '1/2', '1/1', '1/1', '1/1', '1/1', '2/2', '2/2', '1/1', '1/1']

Key Points

  • Counter: Efficiently counts total occurrences of each element
  • Dictionary comprehension: Creates a mapping for tracking current counts
  • String formatting: Combines numerator and denominator with "/" separator
  • Order preservation: Results maintain the original list order

Conclusion

Fractional frequency calculation helps track element occurrence patterns in lists. Use Counter for total counts and a tracking dictionary for current occurrences to build fraction representations efficiently.

Updated on: 2026-03-26T01:16:23+05:30

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements