Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Recap
Using sets to compute unique elements
4. Creating lists with unique elements
Set operations
Simulating machines
Summary

Instruction

Good job! In the previous exercise, we created a set based on a list with duplicate elements. If we want to create a new list without duplicates, we only need one more operation:

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

We first created a set with unique values by using set(cities). Then we created a list based on the set using list(set(cities)). Note that the order of elements in the list may not be the same as in the original list.

Exercise

John, Mark and Stephanie listened to some songs, sometimes a few times to the same song if they liked it :). We stored the songs they listened to in the order they listened to them in a dictionary named songs_in_order. Now we'd like to create a playlist for each listener. In the playlist, each song should occure only once.

Create a dictionary named playlists, in which you will store a list of the songs listened to by John, Mark, and Stephanie (in no particular order). Any song that was played many times should occur only once in the list.