50 XP

15. Remove duplicates

Intermediate lists sets

Write a function unique(items) that takes a list and returns a new list with each item only once. Keep the items in the order they first appear: when an item repeats, keep its first appearance and drop the later ones.

Items can be numbers or strings, mixed in one list. A number and a string are different items, even when they look alike: 7 and "7" both stay. If items is empty, return an empty list [].

Example 1

Inputitems = [3, 1, 3, 2, 1]
Output[3, 1, 2]
ExplanationThe second 3 and the second 1 are dropped.

Example 2

Inputitems = ["b", "a", "b"]
Output["b", "a"]
Explanation"b" comes first, so it stays first.

Example 3

Inputitems = [7, "7", 7]
Output[7, "7"]
ExplanationThe number 7 and the string "7" differ.

Constraints

  • items has 0 to 100 items.
  • Each item is a whole number or a string.

Submit also runs 4 more tests you can't see.

Practises Sets (lesson 12).

Code
def unique(items):
    """Remove repeats, keeping each item's first place.

    items: list of int or str; may be empty
    Returns: list, each item once, in first-seen order
    """
    # Write your code here
    pass