Python: Dictionaries Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the line of code to initialize an empty dictionary called thesaurus?

thesaurus = new Dictionary()

thesaurus = []

thesaurus = {}

thesaurus = empty_dict()

A

thesaurus = {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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”]}

A

{[“apple”, “orange”]: “fruit”, [“broccoli”]: “vegetable”, [“salt”, “paprika”, “saffron”]: “spice”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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)

A

{“executive”: 7, “hopper”: 20, “lovelace”: 6, “pod”: 2, “snooze booth”: 1}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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”]}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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}

A

{“iron spear”: 12, “invisible knife”: 40, “needle of ambition”: 10, “stone glove”: 20, “mithril shield”: 25}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”]))

A

[“hamburger”, “fries”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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)

A

“Moonlight”
“Casey Affleck”
“Emma Stone”
“Zootopia”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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)

A

“Best Picture”
“Best Actor”
“Best Actress”
“Animated Feature”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

{223842: ‘Teddy Bear’, 872921: ‘Concert Tickets’, 320291: ‘Gift Basket’, 412123: ‘Necklace’, 298787: ‘Pasta Maker’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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)

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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])

A

[“hamburger”, “fries”, “soda”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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))

A

20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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])

A

KeyError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly