print specific item in nested objects at given index in python

To print a specific item in a nested object at a given index in Python, we need to traverse the object using indexing. For example, to print the value of a specific key in a dictionary that's nested inside another dictionary at position 0, we can use the following code:

main.py
my_dict = {
    "outer_key": {
        "inner_key": ["value1", "value2", "value3"]
    }
}

value_to_print = my_dict["outer_key"]["inner_key"][0]
print(value_to_print)
168 chars
9 lines

This code will print the value at index 0 of the inner_key list which is "value1".

Similarly, to print the value at index 2 of the inner_key list we will use:

main.py
value_to_print = my_dict["outer_key"]["inner_key"][2]
print(value_to_print)
76 chars
3 lines

This will print the value at index 2 of the inner_key list which is "value3".

gistlibby LogSnag