Python: Creating Dictionaries Flashcards
What is a dictionary ?
an unordered set of key : value pairs
a dictionary begins and ends with what?
( { and } )
each item consist of a ? and a _?__
key and a value
We can have a list or a dictionary as a __?__ of an item in a dictionary, but we cannot use these data types as __?__ of the dictionary.
We can have a list or a dictionary as a VALUE of an item in a dictionary, but we cannot use these data types as KEYS of the dictionary.
Write the code to create an empty dictionary called animals_in_zoo
animals_in_zoo = {}
Add “zebras” to animals_in_zoo as a key with a value of 8.
animals_in_zoo[“zebras”] = 8
If we wanted to add multiple key : value pairs to a dictionary at once, we can use the __?__ method
.update() method
Create a dictionary called drinks_to_caffeine by using a list comprehension that goes through the zipped_drinks list and turns each pair into a key:value item.
drinks_to_caffeine = {key: value for key, value in zipped_drinks}
_?__ cannot be the keys of a dictionary, because they are mutable
Lists cannot be the keys of a dictionary