# Python | List frequency of elements

• Difficulty Level : Easy
• Last Updated : 27 Feb, 2023

Sometimes we have the utility in which we require to find the frequency of elements in the list and the 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 its list occurrences.

## Python3

 `# 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))`

Output :

```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 the Counter, map function performs the task for all sublists and from_iterable function performs using iterators which is faster than list comprehension.

## Python3

 `# 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))`

Output :

```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 #3: Using python dictionary + get() method

Python dictionary provides a get method which returns the value corresponding to the key and if the key does not exist in the dictionary then it provides functionality to create the key and assign it a default value. We will use this functionality of a dictionary.

## Python3

 `d ``=` `{}` `test_list ``=` `[[``3``, ``5``, ``4``],``             ``[``6``, ``2``, ``4``],``             ``[``1``, ``3``, ``6``]]` `for` `x ``in` `test_list:``  ``for` `i ``in` `x:``    ``d[i] ``=` `d.get(i,``0``) ``+` `1` `# Original list``print``(f``"The original list : {test_list}"` `)` `# printing result``print``(f``"The list frequency of elements is : {d}"` `)`

Output:

```The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {3: 2, 5: 1, 4: 2, 6: 2, 2: 1, 1: 1}```

Method #4: Using Pandas

In this method we will use a python module named pandas(You can know more about pandas in this article) to find the frequency of the given data, here below is the code for it.

## Python3

 `import` `pandas as pd` `test_list ``=` `[``3``,``5``,``4``,``3``,``3``,``4``,``5``,``2``]` `df1 ``=` `pd.Series(test_list).value_counts().sort_index().reset_index().reset_index(drop``=``True``)``df1.columns ``=` `[``'Element'``, ``'Frequency'``]` `# Original list``print``(f``"The original list : {test_list}"` `)` `# printing result``print``(f``"The list frequency of elements is :\n {df1.to_string(index=False)}"` `)`

Output:

```The original list : [3, 5, 4, 3, 3, 4, 5, 2]
The list frequency of elements is :
Element  Frequency
2          1
3          3
4          2
5          2```

Method#5Using defaultdict and loop:

Step-by-step algorithm:

1. Import defaultdict from the collection’s module.
2. Initialize the input list test_list
3. Create a defaultdict object res with the default value of 0.
4. Loop over each sublist in test_list.
5. Loop over each element in the sublist.
6. Increment the value of the res dictionary at the key corresponding to the current element by 1.
7. Convert the res dictionary to a regular dictionary and print the result.

## Python3

 `from` `collections ``import` `defaultdict` `# initializing list``test_list ``=` `[[``3``, ``5``, ``4``],``            ``[``6``, ``2``, ``4``],``            ``[``1``, ``3``, ``6``]]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# using defaultdict and loop``# list frequency of elements``res ``=` `defaultdict(``int``)``for` `sublist ``in` `test_list:``    ``for` `elem ``in` `sublist:``        ``res[elem] ``+``=` `1` `# printing result``print``(``"The list frequency of elements is : "` `+` `str``(``dict``(res)))`

Output

```The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {3: 2, 5: 1, 4: 2, 6: 2, 2: 1, 1: 1}```

Time complexity: O(n^2), where n is the number of elements in test_list. This is because there are nested loops over the input list.

Auxiliary space: O(n), where n is the number of elements in test_list. This is because a dictionary is used to store the frequency of each element.

My Personal Notes arrow_drop_up