Aan de slagGa gratis aan de slag

Determining set differences

Another way of comparing sets is to use the difference() method. It returns all the items found in one set but not another. It's important to remember the set you call the method on will be the one from which the items are returned. Unlike tuples, you can add() items to a set. A set will only add items that do not exist in the set.

In this exercise, you'll explore what species had male subjects in our sample, but didn't have female subjects. The set male_penguin_species has been pre-loaded into your workspace.

Deze oefening maakt deel uit van de cursus

Data Types in Python

Cursus bekijken

Oefeninstructies

  • Use a list comprehension to iterate over each penguin in penguins saved as female_species_list:
    • If the the sex of the penguin is 'FEMALE', return the species value.
  • Create a set using the female_species_list as female_penguin_species.
  • Find the difference between female_penguin_species and male_penguin_species. Store the result as differences.
  • Print the differences. This has been done for you, so hit 'Submit Answer' to see the result!

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Use a list comprehension to iterate over each penguin in penguins saved as female_species_list
# If the the sex of the penguin is 'FEMALE', return the species value
female_species_list = [____["____"] ____ ____ ____ ____ ____ ____["____"] == '____']

# Create a set using the female_species_list as female_penguin_species
female_penguin_species = ____(____)

# Find the difference between female_penguin_species and male_penguin_species. Store the result as differences
differences = ____.____(____)

# Print the differences
print(differences)
Code bewerken en uitvoeren