# Python: Get Unique Values From a List With set()

> Remove duplicates from a Python list by converting it to a set, or use dict.fromkeys() to get unique values while preserving the original order

**URL:** https://sentry.io/answers/get-unique-values-from-a-list-in-python/

---

## The Problem

How do I get all of the unique values in a Python list? In other words, how do I remove duplicates from a list?

## The Solution

We can get all the unique values in a Python list by converting the list into a [set](https://docs.python.org/3/tutorial/datastructures.html#sets), which is an unordered data structure with no duplicate elements. Consider the code below:

```python
my_list = [3, 1, 2, 3, 2, 1]

uniques = set(my_list)

print(uniques) # will output {1, 2, 3}
```

From there, we can convert the set back into a list using `list(uniques)`.

This method does not preserve the list's original order. If the order is important, we will need to use a less orthodox approach to the problem. Like values in sets, Python dictionary keys must be unique, and since version 3.7, Python preserves the insertion order of dictionary keys. Therefore, we can get the unique values in our list by converting its values to dictionary keys using [dict.fromkeys](https://docs.python.org/3/library/stdtypes.html?#dict.fromkeys). From there, we convert back to a list. See below:

```python
my_list = [3, 1, 2, 3, 2, 1]

uniques = list(dict.fromkeys(my_list))

print(uniques) # will output [3, 1, 2]
```

---

*Source: [sentry.io/answers/get-unique-values-from-a-list-in-python/](https://sentry.io/answers/get-unique-values-from-a-list-in-python/)*
