0

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?

2
  • 1
    It would help if you posted the actual dictionary. Also if you want to return the key, why not return key. mydict[key] should return the value for that key. Commented Jul 23, 2020 at 0:23
  • "the return(mydict[key]) in my understanding should return the key not the value" - what? No. If you want to return the key, you'd just return key. The dict wouldn't be involved at all. mydict[key] retrieves the value. Commented Jul 23, 2020 at 0:27

3 Answers 3

1

Remember that a dictionary is a map of keys to values, like

{
   'Ireland':   'Dublin',
   'Indonesia': 'Jakarta'
#   ^key         ^value
}

mydict[key] returns the value of the dictionary at index key. If you want the value of the dict at a certain key, you would use mydict[key], so mydict['Ireland'] will return Dublin.

If you just want to return Dublin, you would use return key

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation. I was confused as to the process but it makes more sense now.
1

If you want to print only the key or value, you can use something like this.

a = {'tomato': 'red', 'banana': 'yellow', 'lime': 'green'}
for key,value in a:
    print (key) #will print keys : tomato, banana, lime
    print (value) #will print values : red, yellow, green
    print (a[key]) #will print values : red, yellow, green

If you want keys, just change your code to say

if key in mydict:
    return key
else:
    return 'Not Found'

Also, you can simplify the code further.

return (key) if key in mydict else 'Not Found'

I rewrote your code as follows:

def findDictItem(mydict, key):
# Student code goes here
    return key if key in mydict else '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'))

Output:

banana
Not Found

1 Comment

Thank you for the clarification. I like the way you simplified it. I didnt think to add the if else statements on the return line.
1

Just to make an example, there is the dictionary function get which returns a default value if the key isn't in the dictionary.

# Complete the function to return a dictionary value
# if it exists or return Not Found if it doesn't exist

def findDictItem(mydict, key):
    return mydict.get(key, 'Not found')

1 Comment

This is very useful, I tried to use the .get() a few times but didnt have the correct syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.