Select Page

How to Get Unique Values from List in Python

by | Programming, Python, Tips

Python lists can store duplicate values. If you want to extract the unique values in a list, there are several ways to do this, and this tutorial will go through each of them with code examples.


Get Unique Values from List Using set()

A Python Set is one of the four built-in data types in Python to store collections of data. A set is a collection that is unordered, unchangeable and not indexed. A set can only hold unique values, so we will remove the duplicates from the list if we convert a list to a set. Let’s look at an example of using the set() method to get unique values from a list.

def unique_func(lst):
   
    lst_to_set = set(lst)
   
    unique_list = list(lst_to_set)
   
    unique_list.sort()
   
    for x in unique_list:
   
        print(x)

In the above code, we have a function that takes a list as an argument and converts it to a set using the set() method. We can convert the set back to a list using the list() method. We then use the sort() method to sort the unique values in ascending order. For further reading on sorting a list, go to the article: How to Sort a List of Tuples in Python. To print the unique values from the new list we use a for loop. Let’s define a list of numbers with duplicates and pass it to the above function:

a_list = [10, 20, 30, 30, 30, 10, 20, 40, 50]

print('The unique values from the list is:')

unique_func(a_list)

Let’s run the code to get the output:

10
20
30
40
50

Get Unique Values from List Using numpy.unique()

You can use the numpy.unique() method to get unique values from a numpy array. If you are dealing with a list, you must convert it to a numpy array using the method numpy.array(). Let’s look at an example of using the unique() method:

import numpy as np

def unique_func(lst):

    arr = np.array(lst)

    unique_vals = np.unique(arr)

    for x in unique_vals:

        print(x)

In the above code, we have a function that takes a list as an argument and converts it to a numpy array using np.array(). We then call the np.unique() method, which returns an array of unique values. We then use a for loop to print the unique values to the console. Let’s define a list of numbers with duplicates and pass it to the above function:

a_list = [10, 20, 30, 30, 30, 10, 20, 40, 50]

print('The unique values from the list is:')

unique_func(a_list)

Let’s run the code to get the result:

The unique values from the list is:
10
20
30
40
50

Get Unique Values from List Using collections.Counter()

The Counter method from the collections library counts items in a list. The method returns a dictionary where the keys are the items, and the values are the counts for each item. We can use the * symbol to get all of the keys in the dictionary object, which is equivalent to all unique values in the list. Let’s look at an example of using Counter.

from collections import Counter

def unique_func(lst):

    count = Counter(lst)

    print(*count)

We import the Counter method from the collections library in the above code. We then define a function that takes a list as an argument. Then, we call the Counter method and assign the resultant dictionary to a variable called count. The function then prints the keys of the dictionary to the console. Let’s define a list of numbers with duplicates and pass it to the above function:

a_list = [10, 20, 30, 30, 30, 10, 20, 40, 50]

print('The unique values from the list is:')

unique_func(a_list)

Let’s run the code to get the result:

The unique values from the list is:
10 20 30 40 50

For further reading on counting the occurrences of elements in a list, go to the article: How to Count Occurrences of an Element in a Python List.

Get Unique Values from List Using Iteration

The naive approach to counting the unique value in a list involves list iteration. Let’s look at an example:

def unique_func(lst):

    unique_lst = []

    for x in lst:

        if x not in unique_lst:

            unique_lst.append(x)

    unique_lst.sort()

    for x in unique_lst:

        print(x)

In the above code, the function defines an empty list, which will store the unique values. Then the function uses a for loop to traverse the original list. If the value in the loop is not already in the list for unique values, we append the value to that list. Once the for loop is complete, all unique values will be in the unique_lst. We use the sort() method to sort the unique values in ascending order and then print the values using a for loop. Let’s define a list of numbers with duplicates and pass it to the above function:

a_list = [10, 20, 30, 30, 30, 10, 20, 40, 50]

print('The unique values from the list is:')

unique_func(a_list)

Let’s run the code to get the result:

The unique values from the list is:
10
20
30
40
50

Summary

Congratulations on reading to the end of this tutorial! We have gone through how to get the unique values from a list in Python. The most efficient method is to convert the list to a set, which removes duplicates. You can then turn the set back to a list using the list() method. If you are working with numpy arrays, the most efficient way to get unique values is to use the method numpy.unique(), which returns a numpy array of unique values from the original array.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!