Python Algorithms

Remove Duplicates from a Python List

A set provides fast membership tests, while a result list preserves the order of the first occurrence.

What is Remove Duplicates from a Python List?

A set provides fast membership tests, while a result list preserves the order of the first occurrence.

Remove duplicate list values while preserving their original order.

When should you use it?

  • Practice problem solving and data structures.
  • Process data when every algorithm step must be explicit.
  • Prepare for programming exercises, exams, and interviews.

Example code

Run code →
main.py
def unique_in_order(values):
    seen = set()
    result = []
    for value in values:
        if value not in seen:
            seen.add(value)
            result.append(value)
    return result

print(unique_in_order([3, 1, 3, 2, 1, 4]))

Expected output

[3, 1, 2, 4]

How it works

Each value is checked once. New values enter both the set and output list, giving O(n) average time.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Try empty, single-item, and duplicate-value inputs.
  2. Print state after every step to observe the algorithm.
  3. Benchmark 100, 1,000, and 10,000 items, then compare another approach.
Run in Python IDE →