Let’s check out some exercises that will help understand Nested Data better.

Exercise 6-a

Print 5 by accessing the nested data.


You can chain the bracket notation: [][][] to access the element you need.

ans_1 = nested_lst[1][1]

Exercise 6-b

Print "Z" from the nested data.


You can chain the bracket notation: [][][] to access the element you need.

ans_1 = nested_lst[1][1][0]

Exercise 6-c

What color is the violet?


You can chain the bracket notation: [][][] to access the element you need.

ans_1 = nested_lst[2]["violet"]

Exercise 6-d

Print the values of the "roads" key from the nested dictionary.


If your dictionary’s key is a string you have to use quotes while accessing them.: [“string_key”]

ans_1 = nested_dict["Dakar"]["roads"]

 

Exercise 6-e

Print the first element of the weather for Tokyo.


You can use a chain version of [][] notation with dictionary keys as well.

If your dictionary’s key is a string you have to use quotes while accessing them.: [“string_key”]

ans_1 = nested_dict["Tokyo"]["weather"][0]