Unique elements in order

EasyFunction Programs · 9 of 39

Write a Python function that returns a new list with the unique elements of a list, in order.

Advertisement
Input
[2, 2, 3, 1, 4, 4, 4, 4, 4, 6]
Output
[2, 3, 1, 4, 6]

Approach

  1. Append items not yet seen.

Solution

Python
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.

More function programs

← All 39 Function Programs

Leave a Comment