Posted on:

30 Mar 2021

0

writing functions to iterate over a list of dictionaries

I have a list of dictionaries called reviewers_dicts.

here is a sample output of what 'reviewers_dicts' contains:
In: print(reviewers_dicts[:2])
Out: [{'Username': 'bkpn1412', 'DOB': '31.07.1983', 'State': 'Oregon', 'Reviewed': ['cea76118f6a9110a893de2b7654319c0']},
{'Username': 'gqjs4414', 'DOB': '27.07.1998', 'State': 'Massachusetts', 'Reviewed': ['fa04fe6c0dd5189f54fe600838da43d3']}]

I want to write a function called 'find_user' where it should take one argument and return the dictionary from reviewers_dicts
that contains the given username in its Username field. If the username is not found, it should return None.

Test it by calling it with sample user name = krpz1113

Here was my attempt,

def find_user(Username):
    for k in reviewers_dicts:
        return k

find_user('krpz1113')

#This outputs:
{'Username': 'aaez2213',
'DOB': '06.08.1959',
'State': 'Minor Outlying Islands',
'Reviewed': ['02f7779ab84319cdf752177315b9cf4f',
  '0ae2c0dbb749d34315e7be47da3a7020',
  'c58b4d51ec411b8803472e6ddc320ab6',
  'f89b4212c3123a11674726ebd8c7f25c']}

#I kinda understand why, so I also tried adding if statement, but it only threw errors.

Please help :D

0 answers ( 0 marked as helpful)

Submit an answer