Python: Dictionaries Flashcards
What is the line of code to initialize an empty dictionary called thesaurus?
thesaurus = new Dictionary()
thesaurus = []
thesaurus = {}
thesaurus = empty_dict()
thesaurus = {}
Which of these is an invalid dictionary (will result in a TypeError when trying to run)?
{2: [“apple”, “orange”], 1: [“broccoli”], 3: [“salt”, “paprika”, “saffron”]}
{[“apple”, “orange”]: “fruit”, [“broccoli”]: “vegetable”, [“salt”, “paprika”, “saffron”]: “spice”}
{“fruit”: “apple”, “vegetable”: 100, “spice”: [“salt”, “paprika”, “saffron”]}
{[“apple”, “orange”]: “fruit”, [“broccoli”]: “vegetable”, [“salt”, “paprika”, “saffron”]: “spice”}
What will the following code output?
conference_rooms = [“executive”, “hopper”, “lovelace”, “pod”, “snooze booth”]
capacity = [7, 20, 6, 2, 1]
room_dict = {key:value for key, value in zip(conference_rooms, capacity)}
print(room_dict)
{“executive”: 7, “hopper”: 20, “lovelace”: 6, “pod”: 2, “snooze booth”: 1}
Which of these dictionaries has integers as the keys and strings as the values?
zipcodes = {35801: “Alabama”, “No Value”: “N/A”, “APO”: “Overseas”}
zipcodes = {35801: “Alabama”, 99501: “Alaska”, 97201: “Oregon”, 05751: “Vermont”, 07039: “New Jersey”}
zipcodes = {“Alabama”:35801,”Alaska”:99501, “Oregon”:97201, “Vermont”:05751, “New Jersey”:07039}
zipcodes = {35801: [“Huntsville”, “Montgomery”], 99501: [“Anchorage”], 97201: [“Portland”, “Salem”], 05751: [“Burlington”, “Montpelier”, “Rutland”], 07039: [“Hoboken”]}
zipcodes = {35801: “Alabama”, 99501: “Alaska”, 97201: “Oregon”, 05751: “Vermont”, 07039: “New Jersey”}
👏
Yes! The keys, like 35801, are integers. The values, like “Alabama”, are strings.
What is the value of inventory after this code is run?
inventory = {“iron spear”: 12, “invisible knife”: 30, “needle of ambition”: 10, “stone glove”: 20}
inventory[“invisible knife”] = 40
inventory[“mithril shield”] = 25
{“iron spear”: 12, “invisible knife”: 40, “needle of ambition”: 10, “stone glove”: 20, “mithril shield”: 25}
{“iron spear”: 12, “invisible knife”: 30, “needle of ambition”: 10, “stone glove”: 20, “invisible knife”: 40, “mithril shield”: 25}
{“iron spear”: 12, “invisible knife”: 70, “needle of ambition”: 10, “stone glove”: 20}
{“iron spear”: 12, “invisible knife”: 70, “needle of ambition”: 10, “stone glove”: 20, “mithril shield”: 25}
{“iron spear”: 12, “invisible knife”: 40, “needle of ambition”: 10, “stone glove”: 20, “mithril shield”: 25}
What will the following code output?
combo_meals = {1: [“hamburger”, “fries”], 2: [“hamburger”, “fries”, “soda”], 4: [“veggie burger”, “salad”, “soda”], 6: [“hot dog”, “apple slices”, “orange juice”]}
print(combo_meals.get(3, [“hamburger”, “fries”]))
[“hamburger”, “fries”]
What is the output of the following code?
oscars = {“Best Picture”: “Moonlight”, “Best Actor”: “Casey Affleck”, “Best Actress”: “Emma Stone”, “Animated Feature”: “Zootopia”}
for element in oscars.values():
print(element)
“Moonlight”
“Casey Affleck”
“Emma Stone”
“Zootopia”
What will the following code output?
inventory = {“iron spear”: 12, “invisible knife”: 30, “needle of ambition”: 10, “stone glove”: 20, “the peacemaker”: 65, “demonslayer”: 50}
print(“the peacemaker” in inventory)
True
What is the output of this code?
oscars = {“Best Picture”: “Moonlight”, “Best Actor”: “Casey Affleck”, “Best Actress”: “Emma Stone”, “Animated Feature”: “Zootopia”}
for element in oscars:
print(element)
“Best Picture”
“Best Actor”
“Best Actress”
“Animated Feature”
What is the output of the following code?
raffle = {223842: “Teddy Bear”, 872921: “Concert Tickets”, 320291: “Gift Basket”, 412123: “Necklace”, 298787: “Pasta Maker”}
raffle.pop(561721, “No Value”)
print(raffle)
{223842: ‘Teddy Bear’, 872921: ‘Concert Tickets’, 320291: ‘Gift Basket’, 412123: ‘Necklace’, 298787: ‘Pasta Maker’}
What does the following code output?
inventory = {“iron spear”: 12, “invisible knife”: 30, “needle of ambition”: 10, “stone glove”: 20, “the peacemaker”: 65, “demonslayer”: 50}
print(12 in inventory)
False
What will the following code output?
combo_meals = {1: [“hamburger”, “fries”], 2: [“hamburger”, “fries”, “soda”], 4: [“veggie burger”, “salad”, “soda”], 6: [“hot dog”, “apple slices”, “orange juice”]}
print(combo_meals[2])
[“hamburger”, “fries”, “soda”]
What will the following code output?
inventory = {“iron spear”: 12, “invisible knife”: 30, “needle of ambition”: 10, “stone glove”: 20, “the peacemaker”: 65, “demonslayer”: 50}
print(inventory.get(“stone glove”, 30))
20
What will the following code output?
combo_meals = {1: [“hamburger”, “fries”], 2: [“hamburger”, “fries”, “soda”], 4: [“veggie burger”, “salad”, “soda”], 6: [“hot dog”, “apple slices”, “orange juice”]}
print(combo_meals[3])
KeyError