I would like further explanation as to why the following code prints the value of the key. I am learning Python for a class and Zybooks is terrible at explaining.
# Complete the function to return a dictionary value
# if it exists or return Not Found if it doesn't exist
def findDictItem(mydict, key):
# Student code goes here
if key in mydict:
return (mydict[key])
else:
return 'Not Found'
# expected output: yellow
print(findDictItem({'tomato': 'red', 'banana': 'yellow', 'lime': 'green'} , 'banana'))
# expected output: Not Found
print(findDictItem({'Brazil': 'Brasilia', 'Ireland': 'Dublin', 'Indonesia': 'Jakarta'},'Cameroon'))
The return(mydict[key]), in my understanding, should return the key not the value, but returns they value instead.
Would someone please provide clarity?
return key.mydict[key]should return the value for that key.return key. The dict wouldn't be involved at all.mydict[key]retrieves the value.