1. Learn
  2. /
  3. Courses
  4. /
  5. Writing Efficient Python Code

Exercise

Gathering unique Pokémon

A sample of 500 Pokémon has been created with replacement (meaning a Pokémon could be selected more than once and duplicates exist within the sample).

Three lists have been loaded into your session:

  • The names list contains the names of each Pokémon in the sample.
  • The primary_types list containing the corresponding primary type of each Pokémon in the sample.
  • The generations list contains the corresponding generation of each Pokémon in the sample.

The below function was written to gather unique values from each list:

def find_unique_items(data):
    uniques = []

    for item in data:
        if item not in uniques:
            uniques.append(item)

    return uniques

Let's compare the above function to using the set data type for collecting unique items.

Instructions 1/4

undefined XP
    1
    2
    3
    4
  • Use the provided function to collect the unique Pokémon in the names list. Save this as uniq_names_func.