Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Recap
Using sets to compute unique elements
3. Creating sets from lists
Set operations
Simulating machines
Summary

Instruction

Excellent. One fundamental feature of sets is that they can only contain unique elements. This means that we can use sets to find unique elements in other data structures, such as lists or dictionaries.

cities = ['London', 'Madrid', 'Milan', 'Bordeaux', 'Milan', 'London', 'Lisbon', 'London']
unique_cities = set(cities)

The code above has a list containing city names. We want to only get unique cities from the list, so we create a set from the list using set(cities). At this point, unique_cities contains five elements: London, Madrid, Milan, Bordeaux, and Lisbon.

Exercise

You are given a dictionary (people_cars) that maps people's names to their car make. Create a set named car_makes that contains only the unique car makes found in the people_cars dictionary.

Stuck? Here's a hint!

Use people_cars.values() to get a list of values in the dictionary.