Get Unique Values from a List in Python
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.
Related Articles
Similar Reads
Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python.Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a sin
3 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
range() to a list in Python In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m
2 min read