Get startedGet started for free

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.

This exercise is part of the course

Data Types in Python

View Course

Exercise instructions

  • 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!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code