Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 10: Dictionaries
33 problems · 0 figuresConcepts in Practice 10.1.1 §10.1 source ↗
Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, answer the following questions.
days dictionary object?- a.
"Sunday", "Monday", "Tuesday" - b.
1, 2, 3 - c.
0, 1, 2
Answer
"Sunday", "Monday", "Tuesday""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.
days dictionary object?- a.
0, 1, 2 - b.
1, 2, 3 - c.
"Sunday", "Monday", "Tuesday"
Answer
1, 2, 31, 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.
"Sunday"?- a.
"Sunday" - b.
1 - c.
0
Answer
1days 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.
type(encoding) return?- a.
dictionary - b.
dict - c.
str
Answer
dict 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.
encoding dictionary contains duplicate keys since it has both "a" and "A" as keys.- a.true
- b.false
Answer
"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.true
- b.false
Answer
Concepts in Practice 10.2.1 §10.2 source ↗
- a.
my_dict = () - b.
my_dict = {} - c.
my_dict = []
Answer
my_dict = {}Concepts in Practice 10.2.2 §10.2 source ↗
"one": 1?- a.
my_dict = ("one": 1) - b.
my_dict = ("one", 1) - c.
my_dict = {"one": 1}
Answer
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} - a.1
- b.2
- c.4
Answer
my_dict dictionary. The two pairs are "a": 1 and "b": 2.Concepts in Practice 10.2.4 §10.2 source ↗
- a.
my_dict = [key1:value1, key2:value2] - b.
my_dict = dict(key1=value1, key2=value2) - c.
my_dict = {key1=value1, key2=value2}
Answer
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 ↗
my_dict = dict({"a": 1})
print(my_dict)- a.
{"a": 1} - b.
<class, 'dict'>
- c.
"a" 1
Answer
{"a": 1}my_dict is a dictionary with one key-value pair, "a": 1.Concepts in Practice 10.2.6 §10.2 source ↗
"a": "A" and "b": "B"?- a.
dict(a: A, b: B) - b.
dict([("a", "A"), ("b", "B")]) - c.
dict({"a" = "A", "b" = "B"})
Answer
dict([("a", "A"), ("b", "B")])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.
members["Jaya"]?- a.
0 - b.
None - c.
"Student"
Answer
"Student" "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.
members.get("jaya")?- a.
0 - b.
None - c.
"Student"
Answer
None 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.
members.get("jaya", "does not exist")?- a.
"Student" - b.
"does not exist" - c.
None
Answer
"does not exist"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.
numbers.keys()?- a.
dict_keys - b.
dict_values - c.
list
Answer
dict_keyskeys() 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.
print(numbers.values())?- a.
[1, 2, 3] - b.
dict_keys([1, 2, 3]) - c.
dict_values([1, 2, 3])
Answer
dict_values([1, 2, 3]) 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.
print(list(numbers.keys()))?- a.
["three", "two", "one"] - b.
["one", "two", "three"] - c.
dict_keys(["one", "two", "three"])
Answer
["one", "two", "three"]"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.
"Coconut soup" to "$11" while keeping other items the same?- a.
food = {""Coconut soup": "$15""} - b.
food["Coconut soup"] = 11 - c.
food["Coconut soup"] = "$11"
Answer
food["Coconut soup"] = "$11"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.
"Butter Chicken": "$18" from the food dictionary?- a.
food.remove("Butter Chicken") - b.
del food["Butter Chicken"] - c.
del food.del("Butter Chicken")
Answer
del food["Butter Chicken"] 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.
food dictionary after calling food.update({"Kabob": "$22", "Sushi": "$16"})?- a.
{"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$22", "Sushi": "$16"} - b.
{"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20", "Sushi": "$16"} - c.
{ "Sushi": "$16", "Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20"}
Answer
{"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$22", "Sushi": "$16"}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.
"apple" in fruit_count.keys()?- a.
True - b.
False
Answer
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.
("orange", 5) in fruit_count.items()?- a.
SyntaxError - b.
True - c.
False
Answer
True "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.
"pomegranate" is greater than 0?- a.
fruit_count("pomegranate") > 0 - b.
fruit_count["pomegranate"] > 0 - c.
fruit_count.get("pomegranate" > 0)
Answer
fruit_count["pomegranate"] > 0Concepts in Practice 10.4.4 §10.4 source ↗
- a.
keys() - b.
values() - c.
items()
Answer
values()values() method iterates over values in a dictionary.Concepts in Practice 10.4.5 §10.4 source ↗
fruit_count = {"apple": 2, "orange": 5, "banana": 1}
for key in fruit_count.keys():
print(key, end = " ")- a.
apple orange banana - b.
2 5 1 - c.
("apple", 2) ("orange", 5) ("banana", 1)
Answer
apple orange banana "apple" , "orange" , and "banana" , and prints the keys space-separated.Concepts in Practice 10.4.6 §10.4 source ↗
fruit_count = {"apple": 2, "orange": 5, "banana": 1}
for value in fruit_count.values():
print(value * 2, end = " ")- a.
apple orange banana - b.
2 5 1 - c.
4 10 2
Answer
4 10 2 4 , 10 , and 2 are printed.Concepts in Practice 10.5.1 §10.5 source ↗
- a.A dictionary that contains only integers as keys and values.
- b.A dictionary that contains another dictionary as a value.
- c.A dictionary that contains only strings as keys and values.
- d.A dictionary that contains only lists as keys and values.
Answer
Concepts in Practice 10.5.2 §10.5 source ↗
- a.by using the key of the outer dictionary
- b.by using the key of the inner dictionary
- c.By using both the keys of the outer and inner dictionaries
Answer
Concepts in Practice 10.5.3 §10.5 source ↗
- a.
{key: value} - b.
{key1: {key2: value}} - c.
{key: {value: key2}}
Answer
{key1: {key2: value}}Concepts in Practice 10.5.4 §10.5 source ↗
numbers = [1, 2, 3, 4, 5]
my_dict = {x: x**2 for x in numbers if x % 2 == 0}
print(my_dict)- a.
{1: 1, 3: 9, 5: 25} - b.
{2: 4, 4: 16} - c.
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
Answer
{2: 4, 4: 16}numbers, and associated values are the powers of two.Concepts in Practice 10.5.5 §10.5 source ↗
names = ["Alice ", "Bob ", "Charlie "]
name_lengths = {name: len(name) for name in names}
print(name_lengths)- a.
{ "Alice ": 5, "Bob ": 3, "Charlie ": 7} - b.
{5: "Alice ", 3: "Bob ", 7: "Charlie "} - c.
{ "A ": 5, "B ": 3, "C ": 7}
Answer
{ "Alice ": 5, "Bob ": 3, "Charlie ": 7}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)- a.
{0: 0, 1: 1, 2: 4, 3: 9} - b.
{0: 0, 1: 1, 2: 4} - c.
{1: 1, 2: 4, 3: 9}
Answer
{0: 0, 1: 1, 2: 4, 3: 9}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.