10.1 Dictionary basics
1.
a.
The first item in each key-value pair is the key. Hence
"Sunday", "Monday", "Tuesday"
are key items.
5.
b.
A dictionary cannot have duplicate keys, and
"a"
and
"A"
are two different keys because
"a"
and
"A"
are different characters.
10.2 Dictionary creation
6.
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.
10.3 Dictionary operations
3.
b.
The second argument to the
get()
function will be returned if the key is not found in the dictionary.
5.
c.
The values in the numbers dictionary are
1
,
2
, and
3
. When printing numbers.values()
, dict_values([1, 2, 3])
is printed.
6.
b.
Keys in the dictionary are
"one", "two", "three"
. Using the list()
constructor, a list object containing all keys is returned.
7.
c.
When accessing a dictionary key and assigning a new value, the associated value for the key will be updated.
9.
a.
The
update()
function modifies the value associated with the key
"Kabob"
and adds a new item, "Sushi": "$16"
.
10.4 Conditionals and looping in dictionaries
2.
b.
The key
"orange"
, with associated value
5
, exists in the dictionary and the return value is
True
.
5.
a.
The loop iterates over the keys
"apple"
,
"orange"
, and
"banana"
, and prints the keys space-separated.
10.5 Nested dictionaries and dictionary comprehension
2.
c.
Both outer and inner keys must be used to index into both outer and inner dictionaries, respectively, to obtain an inner value.
3.
b.
A nested dictionary is a value of another dictionary, representing the data in a two-layer structure.
4.
b.
Keys in the dictionary are even values in the list
numbers
, and associated values are the powers of two.
5.
a.
Dictionary keys are string values in the
names
list, and associated values are the length of each string.
6.
a.
The dictionary is created from key values from 0 up to and including 3. The associated values are powers of two of the keys.