Python Practice › Function Programs › Program 9
Unique elements in order
Write a Python function that returns a new list with the unique elements of a list, in order.
Input
[2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
Output
[2, 3, 1, 4, 6]
Approach
- Append items not yet seen.
Solution
def unique(items): result = [] for x in items: if x not in result: result.append(x) return result print(unique([2, 2, 3, 1, 4, 4, 4, 4, 4, 6]))Output
[2, 3, 1, 4, 6]
Learn the concept
New to this topic? Read the lesson, then come back.