Posted on:

30 Mar 2021


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


#This outputs:
{'Username': 'aaez2213',
'DOB': '06.08.1959',
'State': 'Minor Outlying Islands',
'Reviewed': ['02f7779ab84319cdf752177315b9cf4f',

#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