- 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", "[email protected]"], "James": ["241-879-093", "[email protected]"], "Bob": ["987-004-322", "[email protected]"], "Amy": ["340-999-213", "[email protected]"] } Note, that the email is the second element of the list. please provide the solution, im not getting the logic.
7 Answers
+ 7
If we return "Not found" as a list item then we can grab it with -1 index:
contacts = {
"David": ["123-321-88", "[email protected]"],
"James": ["241-879-093", "[email protected]"],
"Bob": ["987-004-322", "[email protected]"],
"Amy": ["340-999-213", "[email protected]"]
}
#your code goes here
name=input()
print(contacts.get(name,["Not found"])[-1])
+ 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
+ 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.. ^
+ 1
contacts = {
"David": ["123-321-88", "[email protected]"],
"James": ["241-879-093", "[email protected]"],
"Bob": ["987-004-322", "[email protected]"],
"Amy": ["340-999-213", "[email protected]"]
}
#your code goes here
name = input()
x = contacts.get(name, "Not found")
if x == "Not found":
print(x)
else:
print(x[1])
0
contacts = {
"David": ["123-321-88", "[email protected]"],
"James": ["241-879-093", "[email protected]"],
"Bob": ["987-004-322", "[email protected]"],
"Amy": ["340-999-213", "[email protected]"]
}
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')
0
contacts = {
"David": ["123-321-88", "[email protected]"],
"James": ["241-879-093", "[email protected]"],
"Bob": ["987-004-322", "[email protected]"],
"Amy": ["340-999-213", "[email protected]"]
}
#your code goes here
name=input()
print(contacts.get(name,["Not found"])[-1])
0
contacts = {
"David": ["123-321-88", "[email protected]"],
"James": ["241-879-093", "[email protected]"],
"Bob": ["987-004-322", "[email protected]"],
"Amy": ["340-999-213", "[email protected]"]
}
user = input()
if user in contacts :
print(contacts[user][1])
else:
print("Not found")