# Python | Extract least frequency element

• Last Updated : 21 Jan, 2023

Sometimes, while working with data, we can have a problem in which we need to extract element which is occurring least number of times in the list. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using defaultdict() + loop The combination of above functions can be used to perform this task. In this, we extract each element’s frequency using defaultdict() and extract the minimum frequency element after traversing the defaultdict.

## Python3

 `# Python3 code to demonstrate working of``# Extract least frequency element``# using defaultdict() + loop``from` `collections ``import` `defaultdict` `# initialize list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``1``, ``3``, ``5``]` `# printing original list``print``("The original ``list` `: " ``+` `str``(test_list))` `# Extract least frequency element``# using defaultdict() + loop``res ``=` `defaultdict(``int``)``for` `ele ``in` `test_list:``   ``res[ele] ``+``=` `1``min_occ ``=` `9999``for` `ele ``in` `res:``    ``if` `min_occ > res[ele]:``        ``min_occ ``=` `res[ele]``        ``tar_ele ``=` `ele` `# printing result``print``("The minimum occurring element ``is` `: " ``+` `str``(tar_ele))`

Output :

```The original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : 4```

Method #2 : Using Counter() This method is similar to above, the difference is that the frequency is stored using Counter() and then we extract element with least frequency.

## Python3

 `# Python3 code to demonstrate working of``# Extract least frequency element``# using Counter()``from` `collections ``import` `Counter` `# initialize list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``1``, ``3``, ``5``]` `# printing original list``print``("The original ``list` `: " ``+` `str``(test_list))` `# Extract least frequency element``# using Counter``res ``=` `Counter(test_list)``tar_ele ``=` `res.most_common()[``-``1``][``0``]` `# printing result``print``("The minimum occurring element ``is` `: " ``+` `str``(tar_ele))`

Output :

```The original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : 4```

Method #3 : Using set() and list comprehension
In this method, we can use set() to get the unique elements from the given list and then use list comprehension to find the least occurring element.

## Python3

 `# Python3 code to demonstrate working of``# Extract least frequency element``# using set() and list comprehension` `# initialize list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``1``, ``3``, ``5``]` `# printing original list``print``(``"The original list : "` `+` `str``(test_list))` `# Extract least frequency element``# using set() and list comprehension``unique_elements ``=` `set``(test_list)``min_occ ``=` `min``([test_list.count(x) ``for` `x ``in` `unique_elements])``tar_ele ``=` `[x ``for` `x ``in` `unique_elements ``if` `test_list.count(x) ``=``=` `min_occ]` `# printing result``print``(``"The minimum occurring element is : "` `+` `str``(tar_ele))``#this code is contributed by edula vinay kumar reddy`

Output

```The original list : [1, 3, 4, 5, 1, 3, 5]
The minimum occurring element is : [4]```

Time Complexity: O(n) where n is the length of the list.
Auxiliary Space: O(n) where n is the length of the list.

My Personal Notes arrow_drop_up