0
print("**PhoneBook**")
phonedict = [
    {"name":"Harry", "number":"054213435"},
    {"name":"Larry", "number":"12345676"},
    {"name":"Harvey", "number":"10101213"},
    {"name":"Ronaldo", "number":"121314121"},
    {"name":'Laith', "number":"0506203880"}
]
contact = input("Which Contact you want: ")
for contact in phonedict:
    print(contact['name'])

yea soo i do that it prints out the name name multiple time i tryed switching it around but that didnt help because it printed out the whole list

i tried impleminting the dict function with for loop but it didnt work i was expecting that it will as the user which contact you want after the contact name is inputed i expecteded to output the number

  • To use the first answer in the dupe, replace your `for` loop with `print(next(c for c in phonedict if c['name'] == contact)['number'])` – Nick Oct 27 '22 at 05:36

1 Answers1

0

phonedict is a list of dictionaries you need to iterate through the list to check the contact exists.

contact = input("Which Contact you want: ")
for item in phonedict:
    if contact == item['name']:
        print(item)
Rahul K P
  • 15,740
  • 4
  • 35
  • 52