Python
Programming
Coding
Data Manipulation
List Operations

Get unique values from a list in python

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Removing duplicates from a Python list is a common preprocessing step for reporting, validation, and feature pipelines. The best method depends on whether order matters, whether values are hashable, and how large the data is. This guide compares practical options and highlights their tradeoffs.

Fastest Simple Case: Hashable Items, Order Not Required

When order is irrelevant and items are hashable, convert to set.

python
values = [4, 1, 2, 1, 4, 3, 2]
unique = list(set(values))
print(unique)

Pros:

  • Usually fastest in pure Python.
  • Minimal code.

Cons:

  • Output order is arbitrary.

Use this for internal math operations where relative order has no meaning.

Preserve Original Order

Most application code needs first-seen order. dict.fromkeys is concise and fast in modern Python.

python
values = [4, 1, 2, 1, 4, 3, 2]
unique = list(dict.fromkeys(values))
print(unique)  # [4, 1, 2, 3]

Why it works:

  • Dictionary keys are unique.
  • Insertion order is preserved in current Python implementations.

Equivalent explicit pattern with seen set is also useful when you need custom side effects.

python
1def unique_preserve_order(items):
2    seen = set()
3    out = []
4    for x in items:
5        if x not in seen:
6            seen.add(x)
7            out.append(x)
8    return out
9
10print(unique_preserve_order(["a", "b", "a", "c", "b"]))

First Unique by Key

Sometimes elements are complex objects and uniqueness should be based on one field.

python
1users = [
2    {"id": 2, "name": "Ana"},
3    {"id": 1, "name": "Sam"},
4    {"id": 2, "name": "Ana v2"},
5]
6
7
8def unique_by(items, key_fn):
9    seen = set()
10    out = []
11    for item in items:
12        k = key_fn(item)
13        if k not in seen:
14            seen.add(k)
15            out.append(item)
16    return out
17
18print(unique_by(users, lambda u: u["id"]))

This keeps first occurrence for each key and is common in API dedup workflows.

Unhashable Items

Lists and dictionaries are unhashable, so set-based methods fail directly.

Example failure:

python
rows = [[1, 2], [1, 2], [2, 3]]
# set(rows) raises TypeError

Solutions:

  • Convert each element to hashable representation such as tuple.
  • Serialize deterministic key for complex structures.
python
rows = [[1, 2], [1, 2], [2, 3]]
unique_rows = list(dict.fromkeys(tuple(r) for r in rows))
print(unique_rows)  # [(1, 2), (2, 3)]

For nested dictionaries, a normalized JSON key can work if key ordering is controlled.

Scaling Considerations

For very large inputs:

  • Hash-based approaches are O(n) average time.
  • Memory usage grows with number of unique elements.

If data exceeds memory, process in chunks and persist seen keys in an external store or probabilistic structure.

Streaming pattern:

python
1def unique_stream(items):
2    seen = set()
3    for x in items:
4        if x in seen:
5            continue
6        seen.add(x)
7        yield x
8
9for v in unique_stream([1, 2, 1, 3, 2, 4]):
10    print(v)

This avoids creating a second large output list immediately.

Benchmarks and Readability

Micro-optimization is rarely needed unless lists are huge. In many codebases, readability wins:

  • dict.fromkeys for order-preserving simple types.
  • set conversion for order-agnostic tasks.
  • explicit helper for custom uniqueness keys.

Benchmark only when dedup is in a measured hot path.

Common Pitfalls

  • Using set and expecting original order. Fix by using dict.fromkeys or seen-set pattern.
  • Applying set to unhashable items. Fix by converting items to hashable keys.
  • Deduplicating dictionaries by full object identity when one field defines uniqueness. Fix by using key-based helper.
  • Assuming dedup always improves performance. Fix by measuring end-to-end impact, including memory overhead.
  • Repeating dedup logic in many files. Fix by centralizing tested utility functions.

Summary

  • Use set for quick unique extraction when order does not matter.
  • Use dict.fromkeys for order-preserving dedup of hashable items.
  • Use key-based helpers for complex object collections.
  • Handle unhashable items by converting to stable hashable keys.
  • Favor clear utility functions and benchmark only for true hot paths.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.