Extract Unique Values from a List of Values in Python
To extract unique values from a list in Python, you can use the set()
function, list comprehension, or dictionary-based methods. In this tutorial, we will explore different techniques to extract unique values with explanations and examples.
Examples
1. Using set()
to Extract Unique Values
The simplest way to get unique values from a list is by converting it into a set. Since sets do not allow duplicates, this method automatically removes any repeated values.
# List with duplicate values
numbers = [1, 2, 2, 3, 4, 4, 5, 6, 6]
# Extracting unique values using set()
unique_numbers = list(set(numbers))
# Printing the unique values
print("Unique values:", unique_numbers)
Explanation:
In this example:
numbers
: A list containing duplicate values.set(numbers)
: Converts the list into a set, automatically removing duplicates.list(set(numbers))
: Converts the set back into a list.
Output:
Unique values: [1, 2, 3, 4, 5, 6]
This method is efficient but does not maintain the original order of elements.
2. Using dict.fromkeys()
to Maintain Order
A dictionary preserves the order of elements since Python 3.7. We can use dict.fromkeys()
to remove duplicates while keeping the original order.
# List with duplicate values
words = ["apple", "banana", "apple", "cherry", "banana", "date"]
# Extracting unique values while maintaining order
unique_words = list(dict.fromkeys(words))
# Printing the unique values
print("Unique values:", unique_words)
Explanation:
dict.fromkeys(words)
: Creates a dictionary where keys are unique values from the list, preserving order.list(dict.fromkeys(words))
: Converts the dictionary keys back to a list.
Output:
Unique values: ['apple', 'banana', 'cherry', 'date']
This approach ensures that the original order of the list is preserved.
3. Using a Loop and if
Condition
We can manually filter unique values by iterating through the list and adding each new value to a new list.
# List with duplicate values
names = ["Alice", "Bob", "Alice", "Charlie", "Bob", "David"]
# Extracting unique values using a loop
unique_names = []
for name in names:
if name not in unique_names:
unique_names.append(name)
# Printing the unique values
print("Unique values:", unique_names)
Explanation:
unique_names
: An empty list to store unique values.- The
for
loop iterates throughnames
, adding only unseen values tounique_names
.
Output:
Unique values: ['Alice', 'Bob', 'Charlie', 'David']
This method works well but is slower than using set()
or dict.fromkeys()
for large lists.
4. Using List Comprehension
List comprehension provides a concise way to filter unique values while maintaining order.
# List with duplicate values
items = [10, 20, 10, 30, 40, 20, 50]
# Extracting unique values using list comprehension
unique_items = []
[unique_items.append(item) for item in items if item not in unique_items]
# Printing the unique values
print("Unique values:", unique_items)
Explanation:
- The list comprehension iterates through
items
, adding only unseen values tounique_items
. - Since lists maintain order, the unique values appear in their first occurrence order.
Output:
Unique values: [10, 20, 30, 40, 50]
This method is functionally similar to using a loop but is more compact.
Conclusion
Python offers several ways to extract unique values from a list:
set()
: The fastest method but does not preserve order.dict.fromkeys()
: Removes duplicates while maintaining order.- Loop with
if
condition: Manually filters unique values. - List Comprehension: A concise alternative to looping.
The best method depends on whether you need to maintain order (dict.fromkeys()
) or optimize performance (set()
).