Python | List frequency of elements
Sometimes we have the utility in which we require to find the frequency of elements in list and solution to this problem has been discussed many times. But sometimes we come across the task in which we require to find the number of lists that particular elements occur. Let’s discuss certain shorthands in which this can be done.
Method #1 : Using Counter() + set() + list comprehension
The combination of the above functions can be used to perform the task. The Counter function does the grouping, set function extracts the distinct elements as keys of dict and list comprehension check for it’s list occurrences.
# Python3 code to demonstrate # list frequency of elements # using Counter() + set() + list comprehension from collections import Counter # initializing list test_list = [[3, 5, 4], [6, 2, 4], [1, 3, 6]] # printing original list print("The original list : " + str(test_list)) # using Counter() + set() + list comprehension # list frequency of elements res = dict(Counter(i for sub in test_list for i in set(sub))) # printing result print("The list frequency of elements is : " + str(res)) |
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2}
Method #2 : Using Counter() + itertools.chain.from_iterable() + map() + set()
The above 4 functionalities can also be combined to achieve this particular task. The set function extracts the dictionary keys formed by Counter, map function performs the task for all sublists and from_iterable function performs using iterators which is faster than list comprehension.
# Python3 code to demonstrate # list frequency of elements # using Counter() + itertools.chain.from_iterable() + map() + set() from collections import Counter from itertools import chain # initializing list test_list = [[3, 5, 4], [6, 2, 4], [1, 3, 6]] # printing original list print("The original list : " + str(test_list)) # using Counter() + itertools.chain.from_iterable() + map() + set() # list frequency of elements res = dict(Counter(chain.from_iterable(map(set, test_list)))) # printing result print("The list frequency of elements is : " + str(res)) |
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2}
Recommended Posts:
- Python | Sort list elements by frequency
- Python | Find sum of frequency of given elements in the list
- Python | Group list elements based on frequency
- Python | Element with largest frequency in list
- Python | Finding frequency in list of tuples
- Python | Delete elements with frequency atmost K
- Python | Sort given list by frequency and remove duplicates
- Python | Find frequency of given character at every position in list of lists
- Python | Merge List with common elements in a List of Lists
- Python | Add list elements with a multi-list based on index
- Python | Sorting list of lists with similar list elements
- Python | Replace elements in second list with index of same element in first list
- Python | Maximum sum of elements of list in a list of lists
- Python | Indices of sorted list of list elements
- Python | Get first and last elements of a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



