- 1

Dictionary functions

You are making a phonebook. The contacts are stored in a dictionary, where the key is the name and the value is a list, representing the number and the email of the contact. You need to implement search: take the name of the contact as input and output the email. If the contact is not found, output "Not found". contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } Note, that the email is the second element of the list. please provide the solution, im not getting the logic.

30th Jun 2021, 4:58 PM
Dharmendra Singh
Dharmendra Singh - avatar
7 Respuestas
+ 8
If we return "Not found" as a list item then we can grab it with -1 index: contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } #your code goes here name=input() print(contacts.get(name,["Not found"])[-1])
2nd Nov 2021, 6:27 AM
Zura Papiashvili
Zura Papiashvili - avatar
+ 2
inputName = input("") if inputName in contacts: print(contacts[inputName][1]) elif inputName not in contacts: print('Not found') Try this And follow me on instagram @aniket_sng
15th Oct 2021, 3:14 PM
ANIKET SINGH
ANIKET SINGH - avatar
+ 1
result = contacts.get(input(), "Not found") if result != "Not found": result = result[1] print(result) I'm sure there is a better implementation, but here is one way.. ^
30th Jun 2021, 5:07 PM
Yahel
Yahel - avatar
+ 1
contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } #your code goes here name = input() x = contacts.get(name, "Not found") if x == "Not found": print(x) else: print(x[1])
15th Aug 2021, 11:07 AM
Jo_Flow
+ 1
contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } #your code goes here name=input() print(contacts.get(name,["Not found"])[-1])
14th Nov 2022, 8:04 AM
Pooja Patel
Pooja Patel - avatar
0
contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } inputName = input("Enter the name to find email to it: ") if inputName in contacts: print(contacts[inputName][1]) elif inputName not in contacts: print('Not found')
7th Aug 2021, 12:12 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
0
contacts = { "David": ["123-321-88", "david@test.com"], "James": ["241-879-093", "james@test.com"], "Bob": ["987-004-322", "bob@test.com"], "Amy": ["340-999-213", "a@test.com"] } user = input() if user in contacts : print(contacts[user][1]) else: print("Not found")
18th Nov 2022, 10:24 PM
Mohamed Gomaa Soliman