Today, we’ll explain 5 simple ways to get unique values from a list in Python. From using sets to more advanced stuff like Functools’s reduce(), we will cover everything to help you handle list operations in Python.
Problem: How to Get Unique Values from a List
Consider a Python list containing one or more duplicate values. The size of the list can vary from small to medium or large. You have to write a Python program to get unique values from the list
List of Solutions
Go through the following 5 methods to easily get unique values from the list.
Sets to get unique values from a list
The most prominent way to get unique elements from a list is using sets in Python. A set is a perfect mathematical apparatus for cleaning a list from duplicates. Check the following example:
# Example 1: Making a unique list using sets
original_list = [1, 2, 3, 2, 4, 5, 1]
unique_set = set(original_list)
unique_list = list(unique_set)
print(unique_list) # Output: [1, 2, 3, 4, 5]
The set() operation easily filters the duplicates and gives you a list with unique values.
Use list comprehension to filter lists
List comprehension is another cool tool in Python to create lists with unique values. Check the code given below for using it to get this task done.
# Example 2: Making a unique list with list comprehensions
original_list = [1, 2, 3, 2, 4, 5, 1]
unique_list = [x for x in original_list if original_list.count(x) == 1]
print(unique_list) # Output: [3, 4, 5]
By calling list comprehensions in your code, you can easily filter out any repeated value from the list.
Dictionary keys to get unique values
Another tricky way to filter a list is by using the Python dictionary. We’ll use its fromkeys() method. It’s not the usual, but it works surprisingly well to achieve our goal:
# Example 3: Making a unique list with dict.fromkeys()
original_list = [1, 2, 3, 2, 4, 5, 1]
unique_dict = dict.fromkeys(original_list)
unique_list = list(unique_dict.keys())
print(unique_list) # Output: [1, 2, 3, 4, 5]
The simple logic behind this is dictionaries can’t have duplicate keys. Hence, we achieve the goal of having a unique list.
Collections class to get unique values
Python has the Counter class in the collections module. It’s handy for counting stuff. Let’s explore this method with the help of the following example.
# Example 4: Making a unique list with collections.Counter
from collections import Counter
original_list = [1, 2, 3, 2, 4, 5, 1]
element_counts = Counter(original_list)
unique_list = [key for key, count in element_counts.items() if count == 1]
print(unique_list) # Output: [3, 4, 5]
The Counter class helps us count things and, in turn, create a unique list.
functools reduce() for unique values
For those who enjoy a bit of a fancy touch with functional programming, we have functools.reduce()
. It might look complex, but it’s a neat way to make a unique list:
# Example 5: Using functools.reduce()
from functools import reduce
original_list = [1, 2, 3, 2, 4, 5, 1]
unique_list = reduce(lambda acc, x: acc + [x] if x not in acc else acc, original_list, [])
print(unique_list) # Output: [3, 4, 5]
This functional approach helps us build a unique list in a short and fancy way.
Tips: Get unique values from a list
Here are some additional tips you may want to consider while choosing from the above methods.
Time Complexity: Pick the Fastest Route
When you want to find unique values from the list in Python, it’s important to think about how long it will take. Using sets, list comprehensions, and the Counter
approach is quick, making them good choices for different situations.
Handling Tricky Elements: Make it Work for All
For elements that aren’t easy to handle, like lists or dictionaries, sets, and dictionaries might not be the best. In those cases, we go for alternatives like list comprehensions or the functools.reduce()
method.
Keep Things in Order: Make Sure It Looks Right
Sometimes, the order of things matters. If it does, use list comprehensions or the functools.reduce()
method is a good call. Sets and dictionaries don’t always keep things in the order you want.
Stay Updated: Using the Latest and Greatest
If you’re using Python 3.9 or newer, there’s this cool |
operator for sets. It’s like a sneak peek into the future:
# Example 6: Using the | operator (Python 3.9+)
original_list1 = [1, 2, 3]
original_list2 = [3, 4, 5]
unique_list = list(set(original_list1) | set(original_list2))
print(unique_list) # Output: [1, 2, 3, 4, 5]
Keep your Python version up to date and use the newest features for cleaner and smarter code.
Must Read –
Using Reduce() for List, String, Tuple With Examples
Python Lambda with Map, filter, Data Science, and Pandas
Python Program to Convert Lists into a Dictionary
List Count Method in Python
How Do You Filter a List in Python?
Before You Leave
So there you have it! We’ve explored different ways to get unique values from the list in Python. Whether you like the simplicity of sets, the straightforwardness of list comprehensions, or the fancy touch of functional programming, there’s a way for you.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.