Reputation: 1169
I have a list called clients_list full of dictionaries such as this:
clients_list =
[
{'John Guy': [28, '03171992', 'Student']},
{'Bobby Jones': [22, '02181982', 'Student']},
{'Claire Eubanks': [18, '06291998', 'Student']},
]
How would I check to see if someone was in this list using the input answer? I have tried the code
elif answer in clients_list:
print(f'{answer} is in our database.')
But it does not seem to work properly.
Upvotes: 3
Views: 10276
Reputation: 788
clients_list = [
{'John Guy': [28, '03171992', 'Student']},
{'Bobby Jones': [22, '02181982', 'Student']},
{'Claire Eubanks': [18, '06291998', 'Student']}]
def get_name(answer):
data = ("No Data Present in DB.",{'Error':'Not Found'})
for index, val in enumerate(clients_list):
if answer in val.keys():
data = (f'{answer} present in database & found at {index}', clients_list[index])
return data
asnwer = input("Enter a Name")
found, value = get_name(asnwer)
print(found, value)
>>>Bobby Jones present in database & found at 1 {'Bobby Jones': [22, '02181982', 'Student']}
Upvotes: 0
Reputation: 7846
You can try doing it using the zip
function:
clients_list = [{'John Guy': [28, '03171992', 'Student']}, {'Bobby Jones': [22, '02181982', 'Student']}, {'Claire Eubanks': [18, '06291998', 'Student']}]
name = "John Guy"
if name in list(zip(*clients_list))[0]:
print(name + " is in database.")
Output:
John Guy is in database.
The list(zip(*clients_list))
will return a list containing a tuple with the names as such:
[('John Guy', 'Bobby Jones', 'Claire Eubanks')]
Then all you do is to take that one tuple of the list using [0]
and check in that tuple if your the name you gave as input exists.
Or alternatively, you can unpack the single tuple of names:
names, = list(zip(*clients_list))
and the use it to check if your name is in there:
if name in names:
print(name + " is in database.")
Upvotes: 0
Reputation: 16942
Suppose answer
contains "John Guy"
. Then this test if answer in clients_list
asks if the string "John Guy"
is in the list of dictionaries, which of course it isn't, because clients_list
is a list of dictionaries, not strings. Now do you see why your test doesn't do what you expect?
This demonstrates juanpa.arrivilaga's point that the data structure doesn't really match what you are doing with it. If you want to do lookups on names, those names should be dictionary keys. Something like
clients_list = {
'John Guy': [28, '03171992', 'Student'],
'Bobby Jones': [22, '02181982', 'Student'],
'Claire Eubanks': [18, '06291998', 'Student'],
}
You might also consider making the dictionary values named tuples instead of lists.
Upvotes: 2
Reputation: 3822
Try this
clients_list = [{'John Guy': [28, '03171992', 'Student']},
{'Bobby Jones': [22, '02181982', 'Student']},
{'Claire Eubanks': [18, '06291998', 'Student']}]
c= 'John Guy'
for item in clients_list:
if c in item:
print c + 'is in our database.'
break
Upvotes: 3
Reputation: 164623
If you want to match on keys, you can use set().union
:
clients_list = [{'John Guy': [28, '03171992', 'Student']},
{'Bobby Jones': [22, '02181982', 'Student']},
{'Claire Eubanks': [18, '06291998', 'Student']}]
x = input('Enter a name:')
if x in set().union(*clients_list):
print('Name found')
else:
print('Name not found')
Upvotes: 0
Reputation: 1
Commas between the list elements.
clients_list =[{'John Guy': [28, '03171992', 'Student']},
{'Bobby Jones': [22, '02181982', 'Student']},
{'Claire Eubanks': [18, '06291998', 'Student']}]
As for the issue, this should work
for d in clients_list:
for person in d.items():
if "John Guy" in person[0]:
print (person[1])
print (person[0]+" is in our database")
Upvotes: 0