Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 10: Dictionaries

33 problems · 0 figures

Concepts in Practice 10.1.1 §10.1 source ↗

Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, answer the following questions.

What are the keys in the days dictionary object?
  1. a.
    "Sunday", "Monday", "Tuesday"
  2. b.
    1, 2, 3
  3. c.
    0, 1, 2

Answer

A. "Sunday", "Monday", "Tuesday"
The first item in each key-value pair is the key. Hence "Sunday", "Monday", "Tuesday" are key items.

Concepts in Practice 10.1.2 §10.1 source ↗

Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, answer the following questions.

What are the values in the days dictionary object?
  1. a.
    0, 1, 2
  2. b.
    1, 2, 3
  3. c.
    "Sunday", "Monday", "Tuesday"

Answer

B. 1, 2, 3
The second item in each key-value pair is the value. Hence 1, 2, 3 are value items.

Concepts in Practice 10.1.3 §10.1 source ↗

Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, answer the following questions.

What is the value associated with key "Sunday"?
  1. a.
    "Sunday"
  2. b.
    1
  3. c.
    0

Answer

B. 1
Based on the days dictionary object, the value assigned to "Sunday" is 1 .

Concepts in Practice 10.1.4 §10.1 source ↗

Given the dictionary object encoding = {"a": 97, "b": 98, "A": 65, "B": 66}, answer the following questions.

What does type(encoding) return?
  1. a.
    dictionary
  2. b.
    dict
  3. c.
    str

Answer

B. dict
The type of a dictionary object is dict .

Concepts in Practice 10.1.5 §10.1 source ↗

Given the dictionary object encoding = {"a": 97, "b": 98, "A": 65, "B": 66}, answer the following questions.

The encoding dictionary contains duplicate keys since it has both "a" and "A" as keys.
  1. a.
    true
  2. b.
    false

Answer

B. false
A dictionary cannot have duplicate keys, and "a" and "A" are two different keys because "a" and "A" are different characters.

Concepts in Practice 10.1.6 §10.1 source ↗

Given the dictionary object encoding = {"a": 97, "b": 98, "A": 65, "B": 66}, answer the following questions.

A dictionary can have duplicate values.
  1. a.
    true
  2. b.
    false

Answer

A. true
A dictionary can have duplicate values, but not duplicate keys.

Concepts in Practice 10.2.1 §10.2 source ↗

What is the correct syntax to create an empty dictionary in Python?
  1. a.
    my_dict = ()
  2. b.
    my_dict = {}
  3. c.
    my_dict = []

Answer

B. my_dict = {}
Curly braces can be used to create an empty dictionary.

Concepts in Practice 10.2.2 §10.2 source ↗

What is the correct syntax to create a dictionary with one item "one": 1?
  1. a.
    my_dict = ("one": 1)
  2. b.
    my_dict = ("one", 1)
  3. c.
    my_dict = {"one": 1}

Answer

C. my_dict = {"one": 1}
{"one": 1} creates a dictionary object with one item "one": 1.

Concepts in Practice 10.2.3 §10.2 source ↗

How many items does the my_dict object contain?

my_dict = {"a": 1, "b": 2}
  1. a.
    1
  2. b.
    2
  3. c.
    4

Answer

B. 2
Two pairs of items exist in the my_dict dictionary. The two pairs are "a": 1 and "b": 2.

Concepts in Practice 10.2.4 §10.2 source ↗

What is the correct syntax to create a dictionary with initial values in Python?
  1. a.
    my_dict = [key1:value1, key2:value2]
  2. b.
    my_dict = dict(key1=value1, key2=value2)
  3. c.
    my_dict = {key1=value1, key2=value2}

Answer

B. my_dict = dict(key1=value1, key2=value2)
dict() function, along with keyword arguments, can be used to create a dictionary object.

Concepts in Practice 10.2.5 §10.2 source ↗

What is the output of the following code?
my_dict = dict({"a": 1})
print(my_dict)
  1. a.
    {"a": 1}
  2. b.
    <class, 'dict'>
  3. c.
    "a"
    1

Answer

A.
{"a": 1}
my_dict is a dictionary with one key-value pair, "a": 1.

Concepts in Practice 10.2.6 §10.2 source ↗

Which option creates a dictionary with two key-value pairs, "a": "A" and "b": "B"?
  1. a.
    dict(a: A, b: B)
  2. b.
    dict([("a", "A"), ("b", "B")])
  3. c.
    dict({"a" = "A", "b" = "B"})

Answer

B. dict([("a", "A"), ("b", "B")])
The dict() function can create a dictionary object from a list of tuples. The first element of each tuple acts as the key, and the second element is the value.

Concepts in Practice 10.3.1 §10.3 source ↗

Given the dictionary members = {"Jaya": "Student", "John": "TA", "Ksenia": "Staff"}, answer the following questions.

What is the output of members["Jaya"]?
  1. a.
    0
  2. b.
    None
  3. c.
    "Student"

Answer

C. "Student"
The value associated with the key "Jaya" is "Student" .

Concepts in Practice 10.3.2 §10.3 source ↗

Given the dictionary members = {"Jaya": "Student", "John": "TA", "Ksenia": "Staff"}, answer the following questions.

What is the output of members.get("jaya")?
  1. a.
    0
  2. b.
    None
  3. c.
    "Student"

Answer

B. None
When a key does not exist in a dictionary, None is returned by default.

Concepts in Practice 10.3.3 §10.3 source ↗

Given the dictionary members = {"Jaya": "Student", "John": "TA", "Ksenia": "Staff"}, answer the following questions.

What is the output of members.get("jaya", "does not exist")?
  1. a.
    "Student"
  2. b.
    "does not exist"
  3. c.
    None

Answer

B. "does not exist"
The second argument to the get() function will be returned if the key is not found in the dictionary.

Concepts in Practice 10.3.4 §10.3 source ↗

Given the dictionary numbers = {"one": 1, "two": 2, "three": 3}, answer the following questions.

What is the output type of numbers.keys()?
  1. a.
    dict_keys
  2. b.
    dict_values
  3. c.
    list

Answer

A. dict_keys
The return type of keys() function is dict_keys.

Concepts in Practice 10.3.5 §10.3 source ↗

Given the dictionary numbers = {"one": 1, "two": 2, "three": 3}, answer the following questions.

What is the output of print(numbers.values())?
  1. a.
    [1, 2, 3]
  2. b.
    dict_keys([1, 2, 3])
  3. c.
    dict_values([1, 2, 3])

Answer

C. dict_values([1, 2, 3])
The values in the numbers dictionary are 1 , 2 , and 3 . When printing numbers.values(), dict_values([1, 2, 3]) is printed.

Concepts in Practice 10.3.6 §10.3 source ↗

Given the dictionary numbers = {"one": 1, "two": 2, "three": 3}, answer the following questions.

What is the output of print(list(numbers.keys()))?
  1. a.
    ["three", "two", "one"]
  2. b.
    ["one", "two", "three"]
  3. c.
    dict_keys(["one", "two", "three"])

Answer

B. ["one", "two", "three"]
Keys in the dictionary are "one", "two", "three". Using the list() constructor, a list object containing all keys is returned.

Concepts in Practice 10.3.7 §10.3 source ↗

Given the dictionary food = {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20"}, answer the following questions.

Which option modifies the value for the key "Coconut soup" to "$11" while keeping other items the same?
  1. a.
    food = {""Coconut soup": "$15""}
  2. b.
    food["Coconut soup"] = 11
  3. c.
    food["Coconut soup"] = "$11"

Answer

C. food["Coconut soup"] = "$11"
When accessing a dictionary key and assigning a new value, the associated value for the key will be updated.

Concepts in Practice 10.3.8 §10.3 source ↗

Given the dictionary food = {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20"}, answer the following questions.

Which option removes the item "Butter Chicken": "$18" from the food dictionary?
  1. a.
    food.remove("Butter Chicken")
  2. b.
    del food["Butter Chicken"]
  3. c.
    del food.del("Butter Chicken")

Answer

B. del food["Butter Chicken"]
Using the del keyword, items can be deleted from a dictionary using the key.

Concepts in Practice 10.3.9 §10.3 source ↗

Given the dictionary food = {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20"}, answer the following questions.

What is the content of the food dictionary after calling food.update({"Kabob": "$22", "Sushi": "$16"})?
  1. a.
    {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$22", "Sushi": "$16"}
  2. b.
    {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20", "Sushi": "$16"}
  3. c.
    { "Sushi": "$16", "Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20"}

Answer

A. {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$22", "Sushi": "$16"}
The update() function modifies the value associated with the key "Kabob" and adds a new item, "Sushi": "$16".

Concepts in Practice 10.4.1 §10.4 source ↗

Given the dictionary fruit_count = {"apple": 2, "orange": 5, "pomegranate": 1}, answer the following questions.

What is the output of "apple" in fruit_count.keys()?
  1. a.
    True
  2. b.
    False

Answer

A. True
"apple" is a key in the dictionary and exists in the returned keys from fruit_count.keys().

Concepts in Practice 10.4.2 §10.4 source ↗

Given the dictionary fruit_count = {"apple": 2, "orange": 5, "pomegranate": 1}, answer the following questions.

What is the output of ("orange", 5) in fruit_count.items()?
  1. a.
    SyntaxError
  2. b.
    True
  3. c.
    False

Answer

B. True
The key "orange" , with associated value 5 , exists in the dictionary and the return value is True .

Concepts in Practice 10.4.3 §10.4 source ↗

Given the dictionary fruit_count = {"apple": 2, "orange": 5, "pomegranate": 1}, answer the following questions.

Which conditional statement checks if the value associated with the key "pomegranate" is greater than 0?
  1. a.
    fruit_count("pomegranate") > 0
  2. b.
    fruit_count["pomegranate"] > 0
  3. c.
    fruit_count.get("pomegranate" > 0)

Answer

B. fruit_count["pomegranate"] > 0
Conditional statements can be written to examine keys and values.

Concepts in Practice 10.4.4 §10.4 source ↗

Which method is used to loop over the values in a Python dictionary?
  1. a.
    keys()
  2. b.
    values()
  3. c.
    items()

Answer

B. values()
The values() method iterates over values in a dictionary.

Concepts in Practice 10.4.5 §10.4 source ↗

What is the output of the following code?
fruit_count = {"apple": 2, "orange": 5, "banana": 1}
for key in fruit_count.keys():
  print(key, end = " ")
  1. a.
    apple orange banana
  2. b.
    2 5 1
  3. c.
    ("apple", 2) ("orange", 5) ("banana", 1)

Answer

A. apple orange banana
The loop iterates over the keys "apple" , "orange" , and "banana" , and prints the keys space-separated.

Concepts in Practice 10.4.6 §10.4 source ↗

What is the output of the following code?
fruit_count = {"apple": 2, "orange": 5, "banana": 1}
for value in fruit_count.values():
  print(value * 2, end = " ")
  1. a.
    apple orange banana
  2. b.
    2 5 1
  3. c.
    4 10 2

Answer

C. 4 10 2
The loop multiplies all values by 2 and prints the output. So, 4 , 10 , and 2 are printed.

Concepts in Practice 10.5.1 §10.5 source ↗

What is a nested dictionary in Python?
  1. a.
    A dictionary that contains only integers as keys and values.
  2. b.
    A dictionary that contains another dictionary as a value.
  3. c.
    A dictionary that contains only strings as keys and values.
  4. d.
    A dictionary that contains only lists as keys and values.

Answer

B. A dictionary that contains another dictionary as a value.
A nested dictionary contains another dictionary.

Concepts in Practice 10.5.2 §10.5 source ↗

How can a value be accessed in a nested dictionary in Python?
  1. a.
    by using the key of the outer dictionary
  2. b.
    by using the key of the inner dictionary
  3. c.
    By using both the keys of the outer and inner dictionaries

Answer

C. By using both the keys of the outer and inner dictionaries
Both outer and inner keys must be used to index into both outer and inner dictionaries, respectively, to obtain an inner value.

Concepts in Practice 10.5.3 §10.5 source ↗

What is the syntax for creating a nested dictionary in Python?
  1. a.
    {key: value}
  2. b.
    {key1: {key2: value}}
  3. c.
    {key: {value: key2}}

Answer

B. {key1: {key2: value}}
A nested dictionary is a value of another dictionary, representing the data in a two-layer structure.

Concepts in Practice 10.5.4 §10.5 source ↗

What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
my_dict = {x: x**2 for x in numbers if x % 2 == 0}
print(my_dict)
  1. a.
    {1: 1, 3: 9, 5: 25}
  2. b.
    {2: 4, 4: 16}
  3. c.
    {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}

Answer

B. {2: 4, 4: 16}
Keys in the dictionary are even values in the list numbers, and associated values are the powers of two.

Concepts in Practice 10.5.5 §10.5 source ↗

What is the output of the following dictionary comprehension?
names = ["Alice ", "Bob ", "Charlie "]
name_lengths = {name: len(name) for name in names}
print(name_lengths)
  1. a.
    { "Alice ": 5, "Bob ": 3, "Charlie ": 7}
  2. b.
    {5: "Alice ", 3: "Bob ", 7: "Charlie "}
  3. c.
    { "A ": 5, "B ": 3, "C ": 7}

Answer

A. { "Alice ": 5, "Bob ": 3, "Charlie ": 7}
Dictionary keys are string values in the names list, and associated values are the length of each string.

Concepts in Practice 10.5.6 §10.5 source ↗

What is the output of the following dictionary comprehension?

my_dict = {i: i**2 for i in range(4)}
print(my_dict)
  1. a.
    {0: 0, 1: 1, 2: 4, 3: 9}
  2. b.
    {0: 0, 1: 1, 2: 4}
  3. c.
    {1: 1, 2: 4, 3: 9}

Answer

A. {0: 0, 1: 1, 2: 4, 3: 9}
The dictionary is created from key values from 0 up to and including 3. The associated values are powers of two of the keys.

Loading chapter 11…

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.