Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Keep only even numbers from a listE02. Transform each item in a listE03. Extract unique elements while keeping orderE

Problem No 3

Extract unique elements while keeping order

Easy

10 minute session

Summary

Return a list of the first occurrence of each element while preserving original order.

Problem statement

Create a function unique_preserve_order(items) that takes a list and returns a new list containing only the first occurrence of each element, preserving the original order. Assume elements are hashable. Example: [1,2,1,3,2] -> [1,2,3].

Task

Implement a function that returns unique items from a list in the order they first appear.

Examples

Remove duplicates

Input

unique_preserve_order([1, 2, 1, 3, 2])

Output

[1, 2, 3]

Explanation

Keep the first occurrence of each element in the order seen.

Input format

A single list of hashable elements (numbers, strings, tuples, etc.).

Output format

A list with duplicates removed, preserving the first occurrence order.

Constraints

Elements are hashable. Input length up to a few thousand elements.

Samples

Sample input 0

['a', 'b', 'a', 'c']

Sample output 0

['a', 'b', 'c']

Explanation 0

Keeps first 'a', 'b', then 'c'.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:43 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.