Unique Elements in a List

Tags: List

Write a Python function that takes a list as input and returns a new list with only the unique elements from the input list. The new list should maintain the order of elements as they originally appeared in the input list.

Example 1:

Input: [1, 2, 2, 3, 4, 4, 4, 5] 
Output: [1, 2, 3, 4, 5]

Example 2:

Input: ['a', 'b', 'b', 'c', 'a', 'd', 'e', 'e'] 
Output: ['a', 'b', 'c', 'd', 'e']

Consider using a different data structure to store the unique elements.

def unique_list(lst):
    seen = []
    for item in lst:
        if item not in seen:
            seen.append(item)
    return seen

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!