Skip to main content

How to Get Unique Values from a List in Python

How to Get Unique Values from a List in Python | Rustcode

How to Get Unique Values from a List in Python

Removing duplicates and getting only unique values from a list is a common task in Python programming. Whether you're cleaning up data, counting items, or just want to see what makes your list special, Python gives you several easy ways to get those unique values. In this article, you'll see practical methods, code examples, outputs, and clear, beginner-friendly explanations.


Why Get Unique Values?

  • Definition: Unique values are the distinct items in a list, with all duplicates removed.
  • Use Cases: Data cleaning, analytics, finding options or categories, and more.
  • Benefits: Helps you focus on what’s important, summarize data, and avoid double-counting.

01. Using set() to Remove Duplicates

The quickest way to get unique values from a list is to convert it to a set. Sets automatically remove duplicates for you.

numbers = [1, 2, 2, 3, 4, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)

Output:

{1, 2, 3, 4, 5}
Explanation:
  • set() removes all repeated items, leaving only one of each.
  • The result is a set, not a list, so the order may not be preserved.
  • If you want a list, just use list(set(numbers)).

02. Preserving Order: dict.fromkeys()

If you want to keep the order of the first appearance of each unique value, use dict.fromkeys() (works in Python 3.7+).

colors = ['red', 'blue', 'red', 'green', 'blue', 'yellow']
unique_colors = list(dict.fromkeys(colors))
print(unique_colors)

Output:

['red', 'blue', 'green', 'yellow']
Explanation:
  • dict.fromkeys() creates a dictionary where keys are unique and order is preserved.
  • Converting back to a list gives you unique values in the order they first appeared.

03. Using a Loop (Manual Approach)

If you want to really see how it works, you can build a list of unique values yourself using a for loop.

animals = ['cat', 'dog', 'cat', 'bird', 'dog', 'fish']
unique_animals = []
for animal in animals:
    if animal not in unique_animals:
        unique_animals.append(animal)
print(unique_animals)

Output:

['cat', 'dog', 'bird', 'fish']
Explanation:
  • Checks if each item is already in the unique list before adding it.
  • Preserves the order of first appearance.
  • Great for learning, but slower for very large lists.

04. List Comprehension for Uniques

For a more compact approach, you can use a list comprehension with a little trick to preserve order:

data = [1, 2, 2, 3, 4, 3, 5]
seen = set()
unique = [x for x in data if not (x in seen or seen.add(x))]
print(unique)

Output:

[1, 2, 3, 4, 5]
Explanation:
  • This checks if an item has been seen before and only adds new items.
  • Efficient and keeps the order of the first occurrence.
  • Combines the speed of sets with the order of lists.

05. Comparing Methods to Get Unique Values in Python

Method Removes Duplicates Preserves Order Best For
set() Yes No Fast, simple, small/medium lists
dict.fromkeys() Yes Yes Ordered unique values
Loop Yes Yes Learning, custom logic
List Comprehension Yes Yes Compact, efficient, order preserved

Conclusion

Getting unique values from a list in Python is simple and flexible. Use set() for quick deduplication, dict.fromkeys() or a manual loop to keep the original order, and list comprehensions for compact, readable code. Choose the method that best fits your needs and the size of your data.

Tip: If you need to preserve order, avoid just using set()—try dict.fromkeys() or a loop!

Comments