Editor

Deduplicate While Preserving Order

Advanced · Write the code · from Python Sets: Unique Collections, Operations, and Use Cases

From the lesson: Python Sets: Unique Collections, Operations, and Use Cases

Imagine you're a teacher taking attendance. A student accidentally signs the attendance sheet twice. Does that mean they're in class twice? Of course not. You only care about who showed up, not how many times they wrote their name.

Read the lesson

Write a function called dedupe(items) that removes duplicates from a list while preserving the original order. Use a set to track which items you've already seen. Return the deduplicated list.

Test it by calling print(dedupe([3, 1, 4, 1, 5, 9, 2, 6, 5, 3])).

def dedupe(items):
# Use a set to track seen items
# Build a new list with only first occurrences
pass
print(dedupe([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]))

More Sets exercises

All Python exercises · 50 practice problems · Data Structures quiz