1

I'm trying to get values from a nested dict via user input. The problem is that the nested dictionaries have generic names (d1,d1, etc.). The user inputs, say, last name and the program returns the email.

I know this is basic, so I apologize in advance. This is what I have so far.

my_dict = {
    'd1':{
        'fname':'john',
        'lname':'doe',
        'age':'26',
        'email':'[email protected]'
    },
    'd2':{
        'fname':'mary',
        'lname':'jane',
        'age':'32',
        'email':'[email protected]'
    }
}

lname = input("enter last name: ")

for emp in my_dict.items():
    print(emp)

Output:

enter last name: john
('d1', {'fname': 'john', 'lname': 'doe', 'age': '26', 'email': '[email protected]'})
('d2', {'fname': 'mary', 'lname': 'jane', 'age': '32', 'email': '[email protected]'})
6
  • You could iterate through my_dict until you find the person you're looking for with .items(). Commented Apr 23, 2022 at 5:39
  • Tried that, but it won't make much difference. It returns each nested dict,, but it won't return the value needed. Commented Apr 23, 2022 at 5:42
  • Why are the dicts stored in a dict? Why not a list? Commented Apr 23, 2022 at 5:51
  • @Bharel I was just trying to make sense of nested dicts. Why would you prefer to use a list of dicts? Is there any advantage? Commented Apr 24, 2022 at 2:52
  • Of course, using a placeholder as a dict name instead of using a list of dictionaries is boh slower and takes more memory. It is also less readable, as someone is trying to understand why use a dict in here in the first place. Commented Apr 24, 2022 at 3:05

2 Answers 2

2

This is a function that takes a last name as input, then iterates over each dictionary (key, value) pair and returns the email as soon as there is a match:

def get_email_from_last_name(last_name):
    for v in my_dict.values():
        if v['lname'] == last_name:
            return v['email']

lname = input("enter last name: ")
email = get_email_from_last_name(lname)
print(email)

prints:

enter last name: doe
[email protected]
Sign up to request clarification or add additional context in comments.

2 Comments

If you don’t need k at all, you could simply iterate through .values() instead of .items()
good point, just adjusted my answer.
0

Per OP's comment, here is a solution with a simple tuple instead:

my_data = (
    {
        'fname':'john',
        'lname':'doe',
        'age':'26',
        'email':'[email protected]'
    },
    {
        'fname':'mary',
        'lname':'jane',
        'age':'32',
        'email':'[email protected]'
    }
)

lname = input("enter last name: ")
for person in my_data:
  if lname == person["lname"]:
    print(person)

You can also map according to the last name, but that is not necessarily a one-to-one relationship.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.