Get Unique Values from a List in Python (original) (raw)

Last Updated : 08 May, 2025

Getting unique values from a list in Python allows you to remove duplicates and work with distinct elements. For example, given the list a = [1, 2, 1, 1, 3, 4, 3, 3, 5], you might want to extract the unique values [1, 2, 3, 4, 5]. There are various efficient methods to extract unique values from a list. Let’s explore different methods to do this efficiently.

Using dict.fromkeys()

This method creates a dictionary where each list item becomes a key and keys can’t repeat, which removes duplicates. Then we turn it back into a list. It’s clean and keeps the original order.

Python `

a = [1, 2, 1, 1, 3, 4, 3, 3, 5] res = list(dict.fromkeys(a)) print(res)

`

**Explanation: dict.fromkeys(a) creates a dictionary with unique elements from **a as keys, removing duplicates. Converting the dictionary keys back to a list gives the unique elements in their original order.

Using collections.OrderedDict

This works like a **regular dictionary but is specially made to remember the order items were added. So, it removes duplicates while keeping the first time each item appeared.

Python `

from collections import OrderedDict

a = [1, 2, 1, 1, 3, 4, 3, 3, 5] res = list(OrderedDict.fromkeys(a)) print(res)

`

**Explanation: OrderedDict.fromkeys(a) creates an OrderedDict with unique elements from **a, removing duplicates and preserving their order. Converting the keys back to a list gives the unique elements in their original order.

Using set conversion

A set automatically removes any repeated items because it only keeps unique values. It’s the fastest way, but it doesn’t keep the original order of the list.

Python `

a = [1, 2, 1, 1, 3, 4, 3, 3, 5] res = list(set(a)) print(res)

`

**Explanation: set(a) creates a set from **a, automatically removing duplicates since sets only store unique elements. Converting the set back to a list gives the unique elements, but the original order is not preserved.

Using for Loop

This method goes through each item in the list one by one and only adds it to the result if it hasn’t been added already. It’s easy to understand and keeps the order, but it can be slower for long lists.

Python `

a = [1, 2, 1, 1, 3, 4, 3, 3, 5] res = []

for item in a: if item not in res: res.append(item) print(res)

`

**Explanation: This code iterates through a, adding items to **res**only if they haven’t been added already, removing duplicates while preserving the original order.