Get Unique Values From a List in Python With Examples

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:

  • 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:

  • Finally, print the new list

Example:

Output:


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:

  • 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:

Output:

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:

  • 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:

Example:

Output:


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

By admin

Leave a Reply