Get Unique Values From a List in Python

Filed Under: Python
Ways To Get Unique Values From A Python List

In this article, we will be understanding 3 ways to get unique values from a Python list. While dealing with a huge amount of raw data, we often come across situations wherein we need to fetch out the unique and non-repeated set of data from the raw input data-set.

The methods listed below will help you solve this problem. Let’s get right into it!


Ways to Get Unique Values from a List in Python

Either of the following ways can be used to get unique values from a list in Python:

  • Python set() method
  • Using Python list.append() method along with a for loop
  • Using Python numpy.unique() method

1. Python Set() to Get Unique Values from a List

As seen in our previous tutorial on Python Set, we know that Set stores a single copy of the duplicate values into it. This property of set can be used to get unique values from a list in Python.

  • Initially, we will need to convert the input list to set using the set() function.

Syntax:

set(input_list_name)
  • As the list gets converted to set, only a single copy of all the duplicate elements gets placed into it.
  • Then, we will have to convert the set back to the list using the below command/statement:

Syntax:

list(set-name)
  • Finally, print the new list

Example:

list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

set_res = set(list_inp) 
print("The unique elements of the input list using set():\n") 
list_res = (list(set_res))
 
for item in list_res: 
    print(item) 

Output:

The unique elements of the input list using set():

25
75
100
20
12

2. Python list.append() and for loop

In order to find the unique elements, we can apply a Python for loop along with list.append() function to achieve the same.

  • At first, we create a new (empty) list i.e res_list.
  • After this, using a for loop we check for the presence of a particular element in the new list created (res_list). If the element is not present, it gets added to the new list using the append() method.

Syntax:

list.append(value)
  • In case, we come across an element while traversing that already exists in the new list i.e. a duplicate element, in such case it is neglected by the for loop. We will use the if statement to check whether it’s a unique element or a duplicate one.

Example:

list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res_list = []

for item in list_inp: 
    if item not in res_list: 
        res_list.append(item) 

print("Unique elements of the list using append():\n")    
for item in res_list: 
    print(item) 
      

Output:

Unique elements of the list using append():

100
75
20
12
25

3. Python numpy.unique() function To Create a List with Unique Items

Python NumPy module has a built-in function named, numpy.unique to fetch unique data items from a numpy array.

  • In order to get unique elements from a Python list, we will need to convert the list to NumPy array using the below command:

Syntax:

numpy.array(list-name)
  • Next, we will use the numpy.unique() method to fetch the unique data items from the numpy array
  • and finally, we’ll print the resulting list.

Syntax:

numpy.unique(numpy-array-name)

Example:

import numpy as N
list_inp = [100, 75, 100, 20, 75, 12, 75, 25] 

res = N.array(list_inp) 
unique_res = N.unique(res) 
print("Unique elements of the list using numpy.unique():\n")
print(unique_res)
      

Output:

Unique elements of the list using numpy.unique():

[12  20  25  75 100]

Conclusion

In this article, we have unveiled various ways to fetch the unique values from a set of data items in a Python list.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages