Intro to Python Dictionaries

Question Click to View Answer

What does the following code print?

player = {"firstName": "Messi", "sport": "soccer"}
print(player["sport"])

"soccer" is printed.

player is a dictionary with two key / value pairs. "firstName" and "sport" are keys. "Messi" and "soccer" are values.

The player["sport"] code fetches the value associated with the "sport" key in the player dictionary.

What does the following code print?

city = {
    "country": "Brazil",
    "hasBeaches": True
}

print(city["hasBeaches"])

True is printed.

The city dictionary is written on multiple lines to enhance the code readability.

The city["hasBeaches"] snippet is used to fetch the value associated with the "hasBeaches" key in the city dictionary.

What does the following code print?

meh = {
    "a": 1,
    "d": 4,
    "e": 99,
}

print(meh["e"])

99 is printed.

The meh["e"] code fetches the value associated with the "e" key in the meh dictionary.

Notice that the final element in the dictionary is also trailed by a comma. Most programming languages don't allow for this syntax. The "traditional" syntax also works.

meh = {
    "a": 1,
    "d": 4,
    "e": 99
}

The Pythonic convention of trailing the final element in a dictionary with a comma is better because it makes it easier to add additional key / value pairs to the dictionary in the future.

What does the following code print?

game = {
    "homeScore": 21,
    "awayScore": 44,
}

print(game["homeTeam"])

This code raises a KeyError exception.

The "homeTeam" key doesn't exist in the game dictionary. Python raises exceptions when you try to access elements that don't exist in a dictionary.

What does the following code print?

fan = {
    "noiseLevel": "loud",
    "cost": 250,
}

print(fan.keys())

dict_keys(['noiseLevel', 'cost']) is printed.

The keys() method returns all of the keys in a dictionary.

What does the following code print?

fan = {
    "noiseLevel": "loud",
    "cost": 250,
}

print(len(fan))

2 is printed.

The len() function returns the number of key / value pairs in a dictionary.

What does the following code print?

building = {
    "numFloors": 3,
    "maxCapacity": 50,
}

del(building["maxCapacity"])

print(building)

{'numFloors': 3} is printed.

The building dictionary initially has two key / value pairs. The del() function is then used to delete the key / value pair with the "maxCapacity" key. The building dictionary only has one key / value pair after the other key / value pair is deleted.

What does the following code print?

neighborhood = {
    "bestRestaurant": "carmen",
    "bestRestaurant": "bruno",
}

print(neighborhood)

{'bestRestaurant': 'bruno'} is printed.

A dictionary cannot have duplicate keys. When keys are duplicated, Python simply ignores all the duplicate key / value pairs except the last one.

What does the following code print?

cat = {}
cat["firstName"] = "garfield"
print(cat)

{'firstName': 'garfield'} is printed.

The cat variable is initially assigned to an empty dictionary. Then the "firstName" key with the "garfield" value is added to the dictionary. This syntax is how to add elements to a dictionary.

What does the following code print?

sticker = {
    "color": "red",
    "picture": "ruby",
}

print("color" in sticker)

True is printed.

The in operator returns True if the key (left operand) is in the dictionary (right operand). This example returns True because the "color" key is in the sticker dictionary.

"size" in sticker would return False because the "size" key is not in the sticker dictionary.

What does the following code print?

bag = dict(
    numPockets = 4,
    brand = "northface",
)

print(bag)

{'numPockets': 4, 'brand': 'northface'} is printed.

The dict() function enables an alternate syntax to create dictionaries. This notation is especially concise when the dictionary keys are strings.

What does the following code print?

dict1 = {"mouth": "big"}
dict2 = {"hair": "tall"}
dict1.update(dict2)

print(dict1)

{'mouth': 'big', 'hair': 'tall'} is printed.

The update() method adds all the key / value pairs from one dictionary into another dictionary. In this example, all the key / value pairs from dict2 are added to dict1.