4

In the given

dict = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}

The questions asks me to print 'me'.

I tried to understand the given keys and values in the dictionary to find any relationship based on the key was unable to do so.

Is there a certain function is should be aware of before diving further?

ggorlen
  • 33,459
  • 6
  • 59
  • 67

2 Answers2

6

Break it down into steps. You started with:

dict = {'d1':[1,2,{'d2':['this is tricky',{'tough':[1,2,['me']]}]}]}
me = 'me'
tough = [1, 2, [me]]
d2 = ['this is tricky', {'tough': tough}]
d1 = [1, 2, {'d2': d2}]
dict = {'d1': d1}

To access me from me from tough

print(tough[2][0])

To access me from tough from d2

print(d2[1]['tough'])

To access me from d2 from d1

print(d1[2]['d2'])

To access me from d1 from dict

print(dict['d1'])

Chaining them all together

dict['d1'][2]['d2'][1]['tough'][2][0]

Note: avoid naming a variable dict, or for that matter, anything that would shadow any of the Python builtins https://docs.python.org/3/library/functions.html

flakes
  • 17,121
  • 8
  • 34
  • 75
-1
dict['d1'][2]['d2'][1]['tough'][2][0]

enter image description here

Horla.li
  • 357
  • 1
  • 9
  • Please do NOT post images of code. Please copy and paste the text instead. Text is more accessible to everybody (be it screen readers, the google bot or people why simply want to copy part of the code) – Sören Jun 03 '22 at 06:13