prapul r
prapul r

Reputation: 23

python search dictionary values and print

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

Answers (4)

Deepak Mishra
Deepak Mishra

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

Mo7ammed 7amad
Mo7ammed 7amad

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

Rounak
Rounak

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')

Results

>>> Please enter the name you would like to search:
>>> Albert
>>> Phone number: 5859715540
>>> Email: [email protected]

Upvotes: 2

Stephen Rauch
Stephen Rauch

Reputation: 49812

A function like:

Code:

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.

Test Code:

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'))

Results:

('Zorg', 8974511147, '[email protected]')
None

Upvotes: 0

Related Questions