Creating a Function to find dictionary entries
"person_forms" is a list of dictionaries.
It contains:
[{'Name': 'Thor','Username': 'bkpn1412', 'DOB': '31.07.1983', 'Country': 'Norway']},
{'Name': 'Franco','Username': 'gqjs4414', 'DOB': '27.07.1998', 'Country': 'Spain'}]
Using a function(which takes one argument), how can I return the dictionary entry which has Username == gqjs4414? (the 2nd entry)
for example: If i call the function with the username value:
find_person('gqjs4414')
Hi Jaiswal!
Thanks for reaching out.
Please follow the order of lectures, quiz questions, and exercises suggested in the course. Once you complete all lectures, exercises, and quiz questions until the end of section Sequences, please watch the Python dictionaries lecture. I suppose the answer you are looking for will be part of the material covered in this lecture.
https://learn.365datascience.com/courses/introduction-to-python/python-dictionaries
Hope this helps but please feel free to get back to us should you need further assistance. Thank you.
Best,
Martin
I've done it ,
Hi Jaiswal!
Thanks for your reply.
I'd say it depends on the context whether you'd need a function to obtain the first dictionary, while you can also just use indexing.
Let's say you want to call your dictionary in the following way:
dict_1 = [{'Name': 'Thor','Username': 'bkpn1412', 'DOB': '31.07.1983', 'Country': 'Norway'},
{'Name': 'Franco','Username': 'gqjs4414', 'DOB': '27.07.1998', 'Country': 'Spain'}]
Then, you can obtain its first element with indexing:
dict_1[1]
Alternatively, here's a function I think can deliver what you are aiming for:
def obtain_first(x):
print (x[1])
Finally, you can execute:
obtain_first(dict_1)
Hope this helps.
Best,
Martin