Now that going on vacation isn't going to be an option for the foreseeable future, you'll have to spice up your quarantine by bringing the destination to you.
Suddenly, you have a craving for tropical fruit salad. Perfect for a hot summer day! You decide to store the fruits you wish to include in your salad in a set.
>>> fruit_salad = {}
>>> fruit_salad.add('mango')
>>> fruit_salad.add('pineapple')
>>> fruit_salad.add('grapes')
>>> fruit_salad
{'mango', 'pineapple', 'grapes'}
The .add() command adds the item to a set.
Note that unlike a list, the items in a set are not ordered. So in this case, it doesn't matter whether you add the mango or the pineapple first, your salad will still taste the same.
Furthermore, the items in a set are unique - that is, there will never be more than one of the same thing. Let's say you really like mango and would like to add some more of it to your salad.
>>> fruit_salad.add('mango')
>>> fruit_salad
{'mango', 'pineapple', 'grapes'}
As you can see, that didn't change anything. So you'll have to keep track on your own how much of each type of fruit you want. We'll dive further into that in a later post (dictionaries).
You call your friend on Zoom to exchange ideas for ingredients. Your friend shares their favorite fruits with you as follows:
>>> friend_salad = {'strawberry', 'kiwi', 'mango', 'apple'}
Which fruits do your recipe and your friend's have in common?
>>> fruit_salad.intersection(friend_salad)
{'mango'}
Which fruits do you like, but not your friend?
>>> fruit_salad.difference(friend_salad)
{'pineapple', 'grapes'}
Can you write a command that tells you which fruits your friend likes, but you don't?
Now, you and your friend decide to combine your recipes together.
>>> fruit_salad.update(friend_salad)
>>> fruit_salad
{'mango', 'strawberry', 'pineapple', 'grapes', 'apple', 'kiwi'}
>>> len(fruit_salad)
6
The .update() method adds every item in the set being passed as an argument (friend_salad0 to the set that .update() is being called on (fruit_salad). The built in method len() in this case returns the number of elements in the set, also known as the cardinality.
If your salad recipe has three fruits and your friend's has four, why does the combined recipe have six fruits and not seven?
Note that the same result could have been accomplished with
>>> big_salad = fruit_salad.union(friend_salad)
>>> big_salad
{'mango', 'strawberry', 'pineapple', 'grapes', 'apple', 'kiwi'}
Note that .update() changes the contents of fruit_salad, but .union() does not, which is why we had to save the result in a new variable.
You are about to serve the fruit salad to your family, when you suddenly remember your dad is allergic to strawberries, so you must remove them.
>>> fruit_salad.remove('strawberry')
>>> fruit_salad
{'mango', 'pineapple', 'grapes', 'apple', 'kiwi'}
Your sister, who wasn't aware you were making fruit salad, tells you she doesn't like raspberries and asks you to remove them as well.
>>> fruit_salad.remove('raspberry')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'raspberry'
This caused an error, because your sister didn't know you never added raspberries to the fruit salad in the first place. And obviously you cannot remove something from a set that isn't there.
If your sister is not comfortable with seeing the error message, use .discard() instead of .remove(); .discard() will NOT print an error message if the item you want to discard doesn't exist.
>>> fruit_salad.discard('raspberry')
>>> fruit_salad
{'mango', 'pineapple', 'grapes', 'apple', 'kiwi'}
However, in practice, .discard is rarely used because the suppression of the error message increases the risk of an uncaught bug in the set itself being propagated through the code.
You can make your code more robust by checking that the element is already inside before discarding.
if 'raspberry' in fruit_salad:
fruit_salad.remove('raspberry')
Your brother doesn't like kiwi. He is familiar with most list methods, including .pop(). He infers that if he calls .pop() on your salad, the kiwi will disappear since it's the "last" element in the set. Before trying this out in your console, do you agree or disagree with your brother?
>>> fruit_salad.pop()
'grapes'
>>> fruit_salad
{'mango', 'pineapple', 'apple', 'kiwi'}
Well, as it turns out, he was wrong. The .pop() method removes an element from a set and returns it. However, your brother forgot that sets, unlike lists, are unordered, so there's no way to know the order in which the items are being stored; instead, Python will remove an item at random.
And now, after the fruit salad has been finished, don't forget to clear the dishes:
>>> fruit_salad.clear()
>>> fruit_salad
set()
The .clear() method removes all the elements from a set, but keeps it in memory. It is not to be confused with
del fruit_salad
which removes fruit_salad from the variable space entirely.