Python - Frequency of Elements from Other List


The frequency of elements consists of two different lists where one list defines the unique element and the other list defines the number of repetitions of the same element w.r.t to the first list. Then use some conditions and operations in the dictionary to set each element of the first list represented by a key whereas the value pair will be represented by counting the total number of repetitions of key element in the second list. In Python, we have some built-in functions such as Counter(), count(), defaultdict(), and, unique() will be used to solve the Frequency of elements from the other list.

Let’s take an example of this.

The given two lists,

lst_1 = [1, 2, 3, 4]

lst_2 = [1, 1, 2, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4]

The final result becomes {1: 3, 2: 4, 3: 1, 4: 5}

Syntax

The following syntax is used in the examples

Counter()

The built-in method Counter() is known as a subclass of a dictionary that keeps track of the number of same items present in the given list.

count()

The built-in method count() accepts a single parameter as input to return the number of items present in the specific list.

defaultdict()

The defaultdict() is an in-built function in Python that automatically generates the key along with default value pairs. This function helps to handle the missing key in the dictionary.

unique()

The unique() is a built-in function in Python that returns the unique elements as a sorted array.

Using Counter()

In the following example, we will start the program by mentioning the library collection and importing the module named Counter that will be used to calculate the frequency of elements. Then create two lists to store in the respective variables i.e. l1 and l2. Next, using the built-in function Counter() it accepts the second list as a parameter to count the number of repetitions of the key and store it in the variable cnt. Then initialize the variable res- use the dictionary comprehension of the variable i set to key and iterate through list l1 to get the total number of counts matches of element w.r.t to l2 and store it in the variable res. Finally, we are printing the result with the help of variable res.

Example

from collections import Counter
# create the list
l1 = [40, 60, 80, 90]
l2 = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]
cnt = Counter(l2)
res = {i: cnt[i] for i in l1}
print("The resulting frequency of key elements:\n", res)

Output

The resulting frequency of key elements:
 {40: 2, 60: 2, 80: 2, 90: 1}

Using Loop and Dictionary

In the following example, start the program by creating two different lists. Then use the empty dictionary that will be used to store the final output of the program. Next, using for loop it iterates through the first list and set the condition as the first element which is equivalent to the second list by counting the element using built-in function count(). Finally, print the result.

Example

l1 = [40, 60, 80, 90]
l2 = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]
# empty dictionary to store the final output
res = {}
for i in l1:
    res[i] = l2.count(i)
print("The resulting frequency of key elements:\n", res)

Output

 The resulting frequency of key elements:
 {40: 2, 60: 2, 80: 2, 90: 1}

Using defaultdict() Function

In the following example, the program uses the collection library and imports the module defaultdict. Then create the two lists to store in the respective variables. Then use the built-in function defaultdict() that is responsible to handle the missing key if the first element of the list doesn’t match the second element of the list and store it in the variable count. Next, using for loop it iterates through the second element of the list and increments by 1 using += operator. Moving to use the dictionary to set the first element list as a key and the second element as a counter. Finally, display the result.

Example

from collections import defaultdict
# create the list
l1 = [40, 60, 80, 90]
l2 = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]
# set the condition for the frequency of elements
count = defaultdict(int)
for i in l2:
    count[i] += 1
# using dictionary
res = {i: count[i] for i in l1}
# display the result
print("The resulting frequency of key elements:\n", res)

Output

 The resulting frequency of key elements:
 {40: 2, 60: 2, 80: 2, 90: 1}

Using numpy library and unique() Function

In the following example, the program uses necessary requirements such as numpy library and two different lists to calculate the frequency of elements. Next, use the built-in function unique() that returns the unique element as a sorted result and stores the same in the two different variables. Now use the dictionary comprehension to set the operation for the frequency of elements by using the built-in function where() and unique(). Finally, it will display the result.

Example

import numpy as np
# create the list
l1 = [40, 60, 80, 90]
l2 = [40, 60, 60, 5, 80, 10, 40, 90, 80, 10, 1]
# set the unique count of each element
unique_l1, counts_l1 = np.unique(l2, return_counts=True)
res = {i: counts_l1[np.where(unique_l1 == i)][0] for i in l1}
# display the output
print("The resulting frequency of key elements:\n", res)

Output

 The resulting frequency of key elements:
 {40: 2, 60: 2, 80: 2, 90: 1}

Conclusion

The frequency of elements refers to the number of elements that appear in the given input list or dataset. The frequency can be determined by counting the number of occurrences present in the list. There are various built-in functions used such as unique(), where(), defaultdict(), etc can be used to solve specific conditions and operations. This type of program applies to some applications such as text mining, market research, quality control, etc.

Updated on: 16-Aug-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements