When it is required to find the element frequencies in the percentage range, the ‘Counter’ is used along with a simple iteration technique.
Example
Below is a demonstration of the same
from collections import Counter my_list = [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] print("The list is :") print(my_list) start, end = 13, 60 my_freq = dict(Counter(my_list)) my_result = [] for element in set(my_list): percent = (my_freq[element] / len(my_list)) * 100 if percent >= start and percent <= end: my_result.append(element) print("The result is : ") print(my_result)
Output
The list is : [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] The result is : [56, 90]
Explanation
The required packages are imported into the environment.
A list is defined and is displayed on the console.
Start and end values are defined.
A dictionary and counter out of the list is prepared and assigned to a variable.
An empty list is defined.
The list is iterated over, and its percentage is found depending on the frequency.
If this value is greater than start and less than end, it is added to the empty list.
This is displayed as output on the console.