Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

16.9: Chapter 10

( \newcommand{\kernel}{\mathrm{null}\,}\)

10.1 Dictionary basics

1. a. The first item in each key-value pair is the key. Hence "Sunday", "Monday", "Tuesday" are key items.
2. b. The second item in each key-value pair is the value. Hence 1, 2, 3 are value items.
3. b. Based on the days dictionary object, the value assigned to "Sunday" is 1 .
4. b. The type of a dictionary object is dict .
5. b. A dictionary cannot have duplicate keys, and "a" and "A" are two different keys because "a" and "A" are different characters.
6. a. A dictionary can have duplicate values, but not duplicate keys.

10.2 Dictionary creation

1. b. Curly braces can be used to create an empty dictionary.
2. c. {"one": 1} creates a dictionary object with one item "one": 1.
3. b. Two pairs of items exist in the my_dict dictionary. The two pairs are "a": 1 and "b": 2.
4. b. dict() function, along with keyword arguments, can be used to create a dictionary object.
5. a. my_dict is a dictionary with one key-value pair, "a": 1.
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

1. c. The value associated with the key "Jaya" is "Student" .
2. b. When a key does not exist in a dictionary, None is returned by default.
3. b. The second argument to the get() function will be returned if the key is not found in the dictionary.
4. a. The return type of keys() function is dict_keys.
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.
8. b. Using the del keyword, items can be deleted from a dictionary using the key.
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

1. a. "apple" is a key in the dictionary and exists in the returned keys from fruit_count.keys().
2. b. The key "orange" , with associated value 5 , exists in the dictionary and the return value is True .
3. b. Conditional statements can be written to examine keys and values.
4. b. The values() method iterates over values in a dictionary.
5. a. The loop iterates over the keys "apple" , "orange" , and "banana" , and prints the keys space-separated.
6. c. The loop multiplies all values by 2 and prints the output. So, 4 , 10 , and 2 are printed.

10.5 Nested dictionaries and dictionary comprehension

1. b. A nested dictionary contains another dictionary.
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.

This page titled 16.9: Chapter 10 is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?