I am self-learning Python3 using the text book: Foundations of Python Principles. The chapter is- Introduction: Nested Data and Nested Iteration.
I am attempting to extract a dictionary value nested inside of a list. The previous question asked me to extract an item from a nested list which was quite straight forward IE:
nested1 = [['a', 'b', 'c'],['d', 'e'],['f', 'g', 'h']]
print(nested1[1][0])
d
This question is asking me to do the same but to get the value from a dictionary nested in a list.
nested2 = [{'a': 1, 'b': 3}, {'a': 5, 'c': 90, 5: 50}, {'b': 3, 'c': "yes"}]
#write code to print the value associated with key 'c' in the second dictionary (90)
Attempted solutions:
print(nested2[2][1])
keyerror: 1
This one got me close but not correct.
print([nested2[2]])
[{'b': 3, 'c': 'yes'}]
Attempting to build on partial success above.
print([nested2[2][1]])
KeyError: 1
Searching online leads me to solutions using but that is a few chapters away. I’m making the assumption this can be done without having to write a loop.