dict.get to pull list item from dict value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

dict.get to pull list item from dict value

Hello! I'm trying to write a dict.get statement that will pull a single element from a dict value that is in the form of a list. If the value were a broken out list, I'd be trying to get list[1] but as an element of a list inside a dict value, I have no idea how to access it with dict.get. This is where I've gotten so far with it: sampleDict = { "name0" : ["phone0", "email0"], "name1" : ["phone1", "email1"], "name2" : ["phone2", "email2"], "name3" : ["phone3", "email3"]} searchQuery = input() print(sampleDict.get(searchQuery, "Not found")) This doesn't return any errors, but it gives the entire list within the value, which isn't what I'm after. I only want the email address. This is what I wrote to solve the exercise and keep going. It works, but it isn't what the lesson really wants to teach. sampleDict = { "name0" : ["phone0", "email0"], "name1" : ["phone1", "email1"], "name2" : ["phone2", "email2"], "name3" : ["phone3", "email3"]} searchQuery = input() if searchQuery in sampleDict: print(sampleDict[searchQuery][1]) else: print("Not found") I don't want to keep using if/else blocks to bypass new material, so I have to figure out the proper syntax for the dict.get statement in this case. I'm puzzled why the syntax that worked in the print statement, didn't work using dict.get.

19th Jun 2022, 3:18 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
5 Answers
+ 3
Jimmy Tyrrell , please link your complete code here, we also need to see the dictionary if this is a sololearn exercise we need to know the tutorial name and / or exercise name / number
19th Jun 2022, 4:30 PM
Lothar
Lothar - avatar
+ 3
Jimmy Tyrrell , using your first code sample, we can do it like this: ... searchQuery = input() #print(sampleDict.get(searchQuery, "Not found")[1]) #(1) this version works well if key is found, but returns only "o" if key is not found print(sampleDict.get(searchQuery, ["","Not found"])[1]) #(2) modifying the output if key is not found should do the job
19th Jun 2022, 8:39 PM
Lothar
Lothar - avatar
+ 1
I was under the impression that would infringe on sololearn's intellectual property, so was generalizing to my specific question without specifying which course or exercise it's from. I've added a sample dictionary in the same format as the one in the exercise.
19th Jun 2022, 4:55 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
+ 1
Lothar , thanks for the help! I'll check it as soon as I get back to my laptop.
19th Jun 2022, 9:39 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
+ 1
Lothar, thanks for the solutions! I have detailed the saga of this exercise in the following code bit: https://code.sololearn.com/cfDjPV3z546t The code is there, but the majority is commentary explaining how I waded through. There's a twist ending for the patient reader :)
20th Jun 2022, 11:21 AM
Jimmy Tyrrell
Jimmy Tyrrell - avatar