3

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.

ZhouQuan
  • 1,037
  • 2
  • 11
  • 19
BoopityBoppity
  • 929
  • 2
  • 9
  • 11
  • related: https://stackoverflow.com/questions/48715552/what-does-x-in-range-y-mean-in-python-3/48715612#48715612 – juanpa.arrivillaga Feb 28 '18 at 09:23
  • 2
    Anyway, what is `answer`? A `str`? Your client_list isn't a very useful data-structure... it should probably be a just a big `dict` object instead of a `list` of `dict` objects. If `answer` is a `str` that you want to match on the keys in the `dict`, use something like `any(answer in d for d in clients_list)` – juanpa.arrivillaga Feb 28 '18 at 09:24
  • answer is a string. I wanted to have dictionaries in the list so that the clients within the client_list could be recalled through indexing as well. Is there any way to have a list of dictionaries and then recall specific dictionaries by inputting the names of the clients(answer)? – BoopityBoppity Feb 28 '18 at 09:32
  • Not without searching, like I showed you. – juanpa.arrivillaga Feb 28 '18 at 09:35

6 Answers6

3

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
khelili miliana
  • 3,730
  • 2
  • 15
  • 28
  • woahh that worked! So does item represent the key values of the dictionaries within the lists? I always thought that item would just represent values within a list. So the value of clients_list would be {'John Guy': [28, '03171992', 'Student']} not just John Guy. – BoopityBoppity Feb 28 '18 at 10:08
2

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.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
0

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')
jpp
  • 159,742
  • 34
  • 281
  • 339
0

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")
Arunabh
  • 1
  • 1
0

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.")
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
0
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']}
Veera Balla Deva
  • 790
  • 6
  • 19