Frequency of Elements from Other List - Python Last Updated : 08 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a list of elements and another list containing specific values and our task is to count the occurrences of these specific values in the first list "a" and return their frequencies. For example: a = [1, 2, 2, 3, 4, 2, 5, 3, 1], b = [1, 2, 3]. Here b contains the elements whose frequency we need to find in a hence the output should be: {1: 2, 2: 3, 3: 2}Using collections.CounterCounter class from the collections module makes it easy to count occurrences in a list and we can use it to get the frequency of all elements in a and then filter only the elements from b. Python from collections import Counter a = [1, 2, 2, 3, 4, 2, 5, 3, 1] b = [1, 2, 3] freq = Counter(a) res = {x: freq[x] for x in b} print(res) Output{1: 2, 2: 3, 3: 2} Explanation:Counter(a) creates a dictionary-like object where keys are elements of a and values are their counts.{x: freq[x] for x in b} filters only the required elements.Using Dictionary ComprehensionWe can use a dictionary comprehension with the count() method to count the occurrences of each element in b directly from a. Python a = [1, 2, 2, 3, 4, 2, 5, 3, 1] b = [1, 2, 3] res = {x: a.count(x) for x in b} print(res) Output{1: 2, 2: 3, 3: 2} Explanation: {x: a.count(x) for x in b} iterates over b and calculates the count of each x in a and count(x) method returns how many times x appears in a.Using map() with count()map() function can be used to apply count() on each element of b creating the frequency dictionary efficiently. Python a = [1, 2, 2, 3, 4, 2, 5, 3, 1] b = [1, 2, 3] res = dict(zip(b, map(a.count, b))) print(res) Output{1: 2, 2: 3, 3: 2} Explanation:map(a.count, b) applies a.count(x) to each x in b and zip(b, map(a.count, b)) pairs each element of b with its count.dict(zip(...)) converts the result into a dictionary.Using for LoopA simple for loop can be used to count occurrences of elements from b in a. Python a = [1, 2, 2, 3, 4, 2, 5, 3, 1] b = [1, 2, 3] res = {} for x in b: res[x] = a.count(x) print(res) Output{1: 2, 2: 3, 3: 2} Explanation:We initialize an empty dictionary result and loop through each element x in b.a.count(x) is used to count occurrences of x in a and the result is stored in result[x]. Comment More infoAdvertise with us Next Article Frequency of Elements from Other List - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Python - List Frequency of Elements We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}.Using collections.Countercollections.Counter class provides a dictionary-like structure that 2 min read Python | Frequency grouping of list elements Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this 6 min read Python - Step Frequency of elements in List Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi 4 min read Python | Group list elements based on frequency Given a list of elements, write a Python program to group list elements and their respective frequency within a tuple. Examples: Input : [1, 3, 4, 4, 1, 5, 3, 1] Output : [(1, 3), (3, 2), (4, 2), (5, 1)] Input : ['x', 'a', 'x', 'y', 'a', 'x'] Output : [('x', 3), ('a', 2), ('y', 1)] Method #1: List c 5 min read Python - Fractional Frequency of elements in List Given a List, get fractional frequency of each element at each position. Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 4]Â Output : ['1/4', '1/3', '2/4', '1/1', '1/1', '2/3', '3/4', '3/3', '4/4']Â Explanation : 4 occurs 1/4th of total occurrences till 1st index, and so on.Input : test_list = [4, 5, 4, 5 min read Like