Open In App

# Python | Get unique values from a list

In this article, we will explore various techniques and strategies for efficiently extracting distinct elements from a given list. By delving into methods ranging from traditional loops to modern Pythonic approaches with Python.

`Input : [1,2, 1, 1, 3, 4, 3, 3, 5 ]Output : [1, 2, 3, 4, 5]  Explaination: The output only contains the unique element from the input list.`

## Get unique values from a list

### Get Unique Values from a List by Traversal of the List

Using traversal, we can traverse for every element in the list and check if the element is in the unique_list already if it is not over there, then we can append it to the unique_list. This is done using one for loop and another if statement which checks if the value is in the unique list or not which is equivalent to another for a loop.

## Python3

 `# function to get unique values``def` `unique(list1):` `    ``# initialize a null list``    ``unique_list ``=` `[]` `    ``# traverse for all elements``    ``for` `x ``in` `list1:``        ``# check if exists in unique_list or not``        ``if` `x ``not` `in` `unique_list:``            ``unique_list.append(x)``    ``# print list``    ``for` `x ``in` `unique_list:``        ``print` `x,`  `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)`  `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output

```the unique values from 1st list is
10 20 30 40
the unique values from 2nd list is
1 2 3 4 5

```

Time Complexity: O(n*n)
Auxiliary Space: O(n)

### Get Unique Values from a List Using Set Method

Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more than once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.

## Python3

 `def` `unique(list1):` `    ``# insert the list to the set``    ``list_set ``=` `set``(list1)``    ``# convert the set to the list``    ``unique_list ``=` `(``list``(list_set))``    ``for` `x ``in` `unique_list:``        ``print` `x,`  `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)`  `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output

```the unique values from 1st list is
40 10 20 30
the unique values from 2nd list is
1 2 3 4 5

```

Time complexity: O(n), where n is the length of a list.
Auxiliary Space: O(n), where n is the length of a list.

### Get Unique Values From a List in Python Using reduce() function

Using Python import reduce() from functools and iterate over all element and checks if the element is a duplicate or unique value. Below is the implementation of the above approach.

## Python3

 `from` `functools ``import` `reduce` `def` `unique(list1):` `    ``# Print directly by using * symbol``    ``ans ``=` `reduce``(``lambda` `re, x: re``+``[x] ``if` `x ``not` `in` `re ``else` `re, list1, [])``    ``print``(ans)`  `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)`  `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output

```the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]

```

### Get Unique Values From a List in Python Using Operator.countOf() method

The ‘unique’ function initializes an empty ‘unique_list’, then iterates through ‘list1’. For each element ‘x’, it employs ‘op.countOf()‘ to check if ‘x’ is present in ‘unique_list’. If not found (count is 0), ‘x’ is appended to ‘unique_list’. The final unique values are printed using a loop. The driver code demonstrates this process for two lists, ‘list1’ and ‘list2’, showcasing the extraction of distinct elements from each list while maintaining their original order.

## Python3

 `import` `operator as op``# function to get unique values`  `def` `unique(list1):` `    ``# initialize a null list``    ``unique_list ``=` `[]` `    ``# traverse for all elements``    ``for` `x ``in` `list1:``        ``# check if exists in unique_list or not``        ``if` `op.countOf(unique_list, x) ``=``=` `0``:``            ``unique_list.append(x)``    ``# print list``    ``for` `x ``in` `unique_list:``        ``print``(x)` `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)` `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output

```the unique values from 1st list is
10
20
30
40

the unique values from 2nd list is
1
2
3
4
5

```

Time Complexity:O(N)
Auxiliary Space: O(N)

### Get Unique Values From a List in Python Using pandas module

The ‘unique’ function utilizes Pandas to create a Series from ‘list1’, then employs ‘drop_duplicates()’ to eliminate duplicates and obtain a list of unique values. Subsequently, it iterates through the unique list and prints each element. The driver code demonstrates the process for two lists, ‘list1’ and ‘list2’, providing distinct values for each list.

## Python3

 `import` `pandas as pd` `# function to get unique values``def` `unique(list1):``    ``unique_list ``=` `pd.Series(list1).drop_duplicates().tolist()``    ``for` `x ``in` `unique_list:``        ``print``(x)` `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)` `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)``#This code is contributed by Vinay Pinjala.`

Output:

`the unique values from 1st list is10203040the unique values from 2nd list is12345`

Time Complexity:O(N)
Auxiliary Space: O(N)

### Get Unique Values From a List Using numpy.unique

Using Python’s import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy.array(list) and then use numpy.unique(x) function to get the unique values from the list. numpy.unique() returns only the unique values in the list.

## Python3

 `# using numpy.unique``import` `numpy as np` `def` `unique(list1):``    ``x ``=` `np.array(list1)``    ``print``(np.unique(x))`  `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)`  `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output:

`the unique values from 1st list is[10 20 30 40]the unique values from 2nd list is[1 2 3 4 5]`

Time complexity: O(nlogn) due to the use of the sorting algorithm used by the numpy.unique() function.
Auxiliary space: O(n) because numpy.unique() function creates a copy of the input array and then sorts it before returning the unique elements.

### Get Unique Values From a List in Python Using collections.Counter()

Using Python to import Counter() from collections print all the keys of Counter elements or we print directly by using the “*” symbol. Below is the implementation of the above approach.

## Python3

 `from` `collections ``import` `Counter` `# Function to get unique values`  `def` `unique(list1):` `    ``# Print directly by using * symbol``    ``print``(``*``Counter(list1))`  `# driver code``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]``print``(``"the unique values from 1st list is"``)``unique(list1)`  `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]``print``(``"\nthe unique values from 2nd list is"``)``unique(list2)`

Output

```the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5

```

Time Complexity: O(n), where n is the number of elements in the input list.
Auxiliary Space : O(n)

### Get Unique Values From a List Using dict.fromkeys()

Using the fromkeys() method of dictionary data structure we can fetch the unique elements.Firstly we need to define a list that consists of duplicate elements.Then we need to use a variable in which we will store the result after using the fromkeys() method.We need to convert that result into a list, as the fromkeys() method is part of the dictionary so by default it returns a dictionary with all the unique keys and None as their values.

## Python3

 `# defining a list which consists duplicate values``list1 ``=` `[``10``, ``20``, ``10``, ``30``, ``40``, ``40``]` `list2 ``=` `[``1``, ``2``, ``1``, ``1``, ``3``, ``4``, ``3``, ``3``, ``5``]` `# storing the result of the fromkeys()``# operation and converting it into list``unique_list_1 ``=` `list``(``dict``.fromkeys(list1))` `unique_list_2 ``=` `list``(``dict``.fromkeys(list2))` `# Printing the final result``print``(unique_list_1,unique_list_2,sep``=``"\n"``)`

Output

```[10, 20, 30, 40]
[1, 2, 3, 4, 5]

```

Time Complexity – O(n)
Space Complexity – O(n)