0
login_DB = [{'ID' : 'xg3571', 'password' : '1234'},
        {'ID': 'elliot', 'password' : 'fake1234'},
        {'ID': 'sweetrin', 'password' : 'lovelive'}
        ]

member = {'ID' : 'xg3571', 'password' : '1234'}

I want to find the dictionary in login_DB, which has same 'ID' key of member, comparing dictionary.

login_account = ...

if login_account == member:
    return True
else:
    return False

I want more pythonic code than this:

login_account = next(dic for dic in login_DB if dic['ID'] == member['ID'])
  • Why not simply store a dictionary mapping id to password and lookup based on that? The whole list thing is just making life more difficult for you – rdas Jul 07 '20 at 17:29
  • 5
    Does this answer your question? [Python list of dictionaries search](https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search) Take a look at PaoloC's answer if you aren't happy with the `next([comprehension])` solution (which ***I*** think is fairly Pythonic, but, to each their own). – matthew-e-brown Jul 07 '20 at 17:31

0 Answers0