What is a nested dictionary in Python?
A
A dictionary inside a list B
A dictionary containing another dictionary as a value C
A dictionary with multiple keys D
A list of keys and values
Analysis & Theory
A nested dictionary is a dictionary where at least one value is another dictionary.
How do you access the value `100` in this dictionary?
data = {'marks': {'math': 100}}
A
data['math'] B
data['marks']['math'] C
data['marks'].math D
data.math.marks
Analysis & Theory
To access nested values, use multiple keys: `data['marks']['math']`.
What will this return?
d = {'a': {'b': {'c': 10}}}
print(d['a']['b']['c'])
A
10 B
{'c': 10} C
Error D
None
Analysis & Theory
This accesses nested keys to reach the value `10`.
What is the output?
data = {'emp': {'name': 'John', 'age': 30}}
print(data['emp']['age'])
A
John B
30 C
emp D
Error
Analysis & Theory
`data['emp']['age']` accesses the nested dictionary and returns the value `30`.
How can you update the nested value `'math'` to 95 in:
d = {'marks': {'math': 90}}
A
d['math'] = 95 B
d['marks']['math'] = 95 C
d.update('math': 95) D
d['marks'].update('math': 95)
Analysis & Theory
Use nested key access to assign a new value: `d['marks']['math'] = 95`.
What is the result of:
d = {'a': {'b': 2}}
print(d.get('a').get('b'))
A
2 B
b C
None D
Error
Analysis & Theory
`get()` is used to access keys safely. Here it returns the value 2.
Which loop structure is best for printing all key-value pairs in a nested dictionary?
A
Single for loop B
While loop C
Nested for loops D
dict.forEach()
Analysis & Theory
Use nested `for` loops to iterate through keys and nested dictionary items.
What will this output?
d = {'a': {'x': 1}, 'b': {'y': 2}}
print(d['b'])
A
2 B
{'y': 2} C
b D
Error
Analysis & Theory
`d['b']` accesses the value associated with `'b'`, which is the nested dictionary `{'y': 2}`.
How do you add a new nested dictionary to an existing dictionary?
A
dict.append({'key': {'subkey': value}}) B
dict['key'] = {'subkey': value} C
dict.add({'key': {'subkey': value}}) D
dict.insert({'key': {'subkey': value}})
Analysis & Theory
Assigning a dictionary as a value to a new key adds a nested dictionary.
What will this output?
d = {'a': {'b': 1}}
d['a']['c'] = 2
print(d)
A
{'a': {'b': 1}} B
{'a': {'b': 1, 'c': 2}} C
{'a': {'c': 2}} D
Error
Analysis & Theory
A new key `'c'` is added inside the nested dictionary under `'a'`.