Let's say you've got a list of restaurants -- maybe you read it from a file. You care about the unique restaurants in the list. The best way to get the unique elements from a list is to turn it into a set:
restaurants = ["McDonald's", "Burger King", "McDonald's", "Chicken Chicken"]
unique_restaurants = set(restaurants)
print(unique_restaurants)
# prints {'Chicken Chicken', "McDonald's", 'Burger King'}
Note that the set is not in the same order as the original list; that is because sets are unordered, just like dict
s.
This can easily be transformed back into a List
with Python's built in list
function, giving another list that is the same list as the original but without duplicates:
list(unique_restaurants)
# ['Chicken Chicken', "McDonald's", 'Burger King']
It's also common to see this as one line:
# Removes all duplicates and returns another list
list(set(restaurants))
Now any operations that could be performed on the original list can be done again.