Home / Fundamentals

# Getting Unique Values from a List in Python

In Python, a list is a collection of values that can be of any data type, including other lists. Sometimes, you may want to extract a set of unique values from a list, which means eliminating duplicates. In this article, we'll look at several ways to get unique values from a list in Python.

## Method 1: Using a set

A set is an unordered collection of unique elements. You can create a set from a list by passing the list to the set constructor. Since a set only allows unique elements, any duplicates in the list will be eliminated. Here's an example:

```# initialize a list with some duplicates
my_list = [1, 2, 3, 3, 4, 4, 5, 5]

# create a set from the list
unique_values = set(my_list)

# print the set
print(unique_values)
```

The output will be {1, 2, 3, 4, 5}. Notice that the duplicates 3, 4, 5 have been eliminated.

## Method 2: Using a for loop

Another way to get unique values from a list is to iterate over the list and add each element to a new list, if it doesn't already exist in the new list. Here's an example:

```# initialize a list with some duplicates
my_list = [1, 2, 3, 3, 4, 4, 5, 5]

# create a new empty list
unique_values = []

# iterate over the list and add each element to the new list
# if it doesn't already exist in the list
for element in my_list:
if element not in unique_values:
unique_values.append(element)

# print the new list
print(unique_values)
```

The output will be the same as before: [1, 2, 3, 4, 5].

## Method 3: Using a list comprehension

A list comprehension is a concise way to create a new list from an existing list in a single line of code. You can use a list comprehension to get unique values from a list by iterating over the list and adding each element to the new list if it doesn't already exist. Here's an example:

```# initialize a list with some duplicates
my_list = [1, 2, 3, 3, 4, 4, 5, 5]

# create a new list using a list comprehension
unique_values = [element for element in my_list if element not in unique_values]

# print the new list
print(unique_values)
```

Again, the output will be the same: [1, 2, 3, 4, 5].

## Method 4: Using the itertools module

The itertools module is a Python module that provides a variety of functions for working with iterators. One of these functions is unique_everseen, which returns an iterator that returns unique elements in the order they were first seen. Here's an example:

```# import the itertools module
import itertools

# initialize a list with some duplicates
my_list = [1, 2, 3, 3, 4, 4, 5, 5]

# use the unique_everseen function to get an iterator of unique elements
unique_values_iterator = itertools.unique_everseen(my_list)

# create a new list from the iterator
unique_values = list(unique_values_iterator)

# print the new list
print(unique_values)
```

The output will be the same as before: [1, 2, 3, 4, 5].