Reputation: 23
dictionary = {'Name':['Sam','Rafael','Albert','Prapul','Zorg','Peter','Sandy','Kristena','Noah','William','Alexander'],
'Number':[9842657266,2548759249,5859715540,9874515875,8974511147,9874447574,5987415877,8898874714,9985852124,
8015005998,9633215749],
'Email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]']}
search = input('Please enter the name you would like to search:')
if search in dictionary.keys():
#print user details
else:
print ('User Not Found')
i need to search specific user in the dictionary and print the username phone number and email address
Upvotes: 1
Views: 222
Reputation: 91
With little modification in Stephen code:
def search(keyword):
for x in zip(*(dictionary[key] for key in (dictionary.keys()))):
if x[0] == keyword:
return x[1],x[2]
Upvotes: 0
Reputation: 87
Your question has been resolved ,But I have another logic :
dictionary = {'Sam':[9842657266,'[email protected]'],'alex':[2548759249,'[email protected]']}
search = input('Please enter the name you would like to search : ')
if search in dictionary.keys():
#here your condition
print('Phone Number :-> ' , dictionary[search][0] ) #index 0 is Phone number
print('Email :-> ' , dictionary[search][1]) #index 1 is email
else :
print('User Not Found')
Upvotes: 0
Reputation: 806
You can search specific user in the dictionary and print the username phone number and email address in the following way:
dictionary = {'Name': ['Sam','Rafael','Albert','Prapul','Zorg','Peter','Sandy','Kristena','Noah','William','Alexander'],
'Number':[9842657266,2548759249,5859715540,9874515875,8974511147,9874447574,5987415877,8898874714,9985852124,
8015005998,9633215749],
'Email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]']}
search = input('Please enter the name you would like to search:\n')
index = -1
for i in range(len(dictionary['Name'])):
if dictionary['Name'][i] == search:
index = i
if index >= 0:
print('Phone number:', dictionary['Number'][index])
print('Email:', dictionary['Email'][index])
else:
print('User Not Found')
>>> Please enter the name you would like to search:
>>> Albert
>>> Phone number: 5859715540
>>> Email: [email protected]
Upvotes: 2
Reputation: 49812
A function like:
def find_name(name_to_find):
return next((x for x in zip(*(
dictionary[key] for key in ('Name', 'Number', 'Email')))
if x[0] == name_to_find), None)
will do that.
dictionary = {'Name':['Sam','Rafael','Albert','Prapul','Zorg','Peter','Sandy','Kristena','Noah','William','Alexander'],
'Number':[9842657266,2548759249,5859715540,9874515875,8974511147,9874447574,5987415877,8898874714,9985852124,
8015005998,9633215749],
'Email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','[email protected]']}
print(find_name('Zorg'))
print(find_name('Junk'))
('Zorg', 8974511147, '[email protected]')
None
Upvotes: 0