How to Extract Unique Values from a List in Python


By squashlabs, Last Updated: November 2, 2023

How to Extract Unique Values from a List in Python

To extract unique values from a list in Python, you can use several approaches. In this answer, we will cover two popular methods: using the set function and using list comprehension.

Method 1: Using the set() function

One simple and efficient way to extract unique values from a list is by using the set() function. The set() function takes an iterable (such as a list) as input and returns a new set that contains only unique elements. Here’s an example:

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_values = set(my_list)


{1, 2, 3, 4, 5}

In this example, the set() function is called with my_list as the input, and the returned set is stored in the unique_values variable. The print() function is then used to display the unique values.

Using the set() function is a straightforward and concise method to extract unique values from a list. However, it is important to note that the order of the elements may not be preserved since sets are an unordered collection of unique elements.

Related Article: How To Convert a Dictionary To JSON In Python

Method 2: Using list comprehension

Another approach to extract unique values from a list is by using list comprehension. List comprehension provides a concise and readable way to create a new list based on an existing list. Here’s an example:

my_list = [1, 2, 2, 3, 4, 4, 5]
unique_values = [x for x in my_list if my_list.count(x) == 1]


[1, 3, 5]

In this example, a list comprehension is used to iterate over each element x in my_list. The if condition my_list.count(x) == 1 checks if the count of x in my_list is equal to 1, indicating that it is a unique value. Only the unique values are added to the unique_values list.

Using list comprehension allows for more flexibility in filtering the unique values. For example, you can modify the if condition to extract unique values based on specific criteria.

Comparison between the methods

Both methods described above are valid ways to extract unique values from a list in Python. However, there are some differences to consider when choosing between them.

– The set() function approach is generally faster and more concise for simple cases where the order of elements doesn’t matter. It automatically removes duplicate values and returns a set, which is an unordered collection of unique elements.
– The list comprehension approach offers more flexibility and control over the filtering of unique values. It allows you to define custom conditions and preserve the order of elements in the original list. However, it may be slower for large lists due to the repeated use of the count() function.

It is important to choose the method that best suits your specific use case, taking into account factors such as performance requirements, order preservation, and the complexity of filtering conditions.

Related Article: How to Sort a Dictionary by Key in Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Remove a Key from a Python Dictionary

Removing a key from a Python dictionary is a common task in programming. This guide provides step-by-step instructions on how to achieve this using the del statement,... read more

How to Remove an Element from a List by Index in Python

A guide on removing elements from a Python list by their index. Methods include using the 'del' keyword, the 'pop()' method, the 'remove()' method, list comprehension,... read more

How to Solve a Key Error in Python

This article provides a technical guide to resolving the KeyError issue in Python. It covers various methods such as checking if the key exists before accessing it,... read more

How to Read a File Line by Line into a List in Python

Reading a file line by line into a list in Python is a common task for many developers. In this article, we provide a step-by-step guide on how to accomplish this using... read more

How to Add New Keys to a Python Dictionary

Adding new keys and their corresponding values to an existing Python dictionary can be achieved using different methods. This article provides a guide on two popular... read more

How to Check If Something Is Not In A Python List

This article provides a guide on using the 'not in' operator in Python to check if an item is absent in a list. It covers the steps for using the 'not in' operator, as... read more