+ 1

There is a dictionary called pairs. How to use get() to print the third element in "orange"?

pairs = {1: "apple", "orange": [2, 3, 4], True: False, 12: "True", }

7th Nov 2021, 6:38 PM
Neethu Nath
Neethu Nath - avatar
2 Respuestas
+ 4
You can try this print( pairs.get( "orange" )[2] ) BUT I have to warn you, since the dict item value here is an iterable( a `list`), it is important to have a valid index to be uaed for refering any of its items. So a safer way would be to check whether the index was within the range of the iterable's length, before proceeding with using the index to obtain an item from the iterable (dict item value).
7th Nov 2021, 6:52 PM
Ipang
+ 1
Thank you Ipang
7th Nov 2021, 7:08 PM
Neethu Nath
Neethu Nath - avatar