Skip to content
C

Remove Duplicates, Preserve Order

Mediumpython

Given a list of numbers that may contain duplicates, print a new list with duplicates removed, keeping the original order of first appearance.

Why not just use set()? converting straight to a set removes duplicates but loses the original order, since sets are unordered. You need to track what's already been seen while keeping the original sequence.

Approach: loop through the numbers, keep a set of values seen so far, and only add a number to the result list the first time it appears.

Input: One line: space-separated whole numbers.

Output: One line: the numbers with duplicates removed, printed as a Python list (in order of first appearance).

Input (stdin)

Output

Run your code to see output here...