Lesson guide
What this Python exercise practices
Extract unique elements while keeping order is a beginner practice lesson that focuses on lists, iteration, filtering. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- List values
- Basic indexing
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Related public exercises
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'.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.