An email import should preserve the order in which addresses first appeared.
What You Need to Understand
A dictionary preserves insertion order, so list(dict.fromkeys(values)) removes duplicates while retaining the first occurrence of each hashable value. An explicit set-based loop gives more control.
When to Use It
An email import should preserve the order in which addresses first appeared.
Step-by-Step Python Example
addresses = ["[email protected]", "[email protected]", "[email protected]"]
unique_addresses = list(dict.fromkeys(addresses))
print(unique_addresses)
How the Example Works
Dictionary keys cannot be repeated. The first occurrence determines the position of each unique address.
Example Output
['[email protected]', '[email protected]']
[4, 2, 1]
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
items = [4, 2, 4, 1, 2]
print(list(dict.fromkeys(items)))
Important Behavior and Edge Cases
Elements must be hashable. Normalizing addresses before deduplicating requires deliberate domain-specific rules, not blind lowercasing of every component.
Try It Yourself
Remove repeated product IDs while keeping first-occurrence order.
Common Beginner Mistakes
- Elements must be hashable. Normalizing addresses before deduplicating requires deliberate domain-specific rules, not blind lowercasing of every component.
- Depending on set iteration order or using a set when duplicates and original positions must be preserved.
Key Takeaways
- A dictionary preserves insertion order, so
list(dict.fromkeys(values))removes duplicates while retaining the first occurrence of each hashable value. - Dictionary keys cannot be repeated.
- Elements must be hashable.