How do I select something from a dictionary within a dictionary using an if statement?


languages = {
    'jen': {
        'specializes': ['python', 'rust'],
        'favorite': 'python',
        'programming_experience': '11 years'
    },

    'sarah': {
        'specializes': ['c', 'css', 'cobol'],
        'favorite': 'css',
        'programming_experience': '4 years',
    },

   'edward': {
       'specializes':['rust, go'],
        'favorite': 'go',
        'programming_experience': '5 years',
   },

   'phil': {
       'specializes': ['python', 'haskell', 'r'],
        'favorite': 'r',
        'programming_experience': '19 years',
   }
}

for programmer, programmer_info in languages.items():
    if programmer_info in languages == ['python']:
        print(f"{programmer.title()} will start the web app tonight using Python")

In the last exercise of one of the chapters in the Python Crash Course it said to extend on a program in the chapter. I decided to have dictionaries inside a dictionary. The first thing I thought of to do with it is to select ‘python’ in an if statement and print something different from the rest. I tried stuff like what I posted above, “if programmer_info in languages == [‘python’]:”, which doesn’t work. It’s good I tried something with more complexity, but I haven’t found an answer to this yet. Any help would be appreciated.

What exactly do you want to check? (Say it in English, then we can help with Python)

1 Like

if 'python' in programmer_info['specializes']: ?

If a programmer, “phil” for example, “specializes” in ‘python’, I want to print a message to only those programmers.

I tried if programmer_info[specializes] in languages == [‘python’]:, but that didn’t work. Not that I expected it to because I have no idea what to do really.

That works! Thank you. Can you explain why it does? I thought I would have had to choose the key first in the statement.

You’re using the items method, which yields the key and the value of each entry, and the value is the subdictionary.

Try this to see what’s happening:

for programmer, programmer_info in languages.items():
    print(f'{programmer=}, {programmer_info=}')
1 Like

Okay. Now you can break the problem down into two steps.

  1. What does Phil specialize in? What code would you use to find that out? What sort of result would you expect to get?

  2. Given that result, how do you know whether Python is one of the specialties? What code would you use to check that result? (Hint: you were on the right track trying to use the word in for the code. Can you think of a way to use the word in when you use English words to describe this step?)

When we answer these questions, we find out:

  1. Once we have Phil’s programmer_info, programmer_info['specializes'] tells us what Phil specializes in. We get a list of strings.

  2. We want to know whether Python - i.e., the string 'python' - is in that list of strings. So that is how we write it: 'python' in programmer_info['specializes'].

I’m not sure what you mean.

If you mean “I thought that I can only put programmer_info['specializes'] on the left-hand side of in, because it’s choosing a key”, then I can’t understand why you expect such a restriction. It’s the same thing as how you can write 1 * (2 + 3), and are not forced to write (2 + 3) * 1.

If you mean “I don’t think that this code is choosing a key”, then I don’t understand what you think “choosing a key” means. Where the code says programmer_info['specializes'], 'specializes' is a key, and you have “chosen” it by typing an apostrophe, the letter s, etc. into your source code.

If you mean “I thought I had to put the thing I’m looking for on the right-hand side of in, and the thing I want to search on the left-hand side” - that also doesn’t make any sense, because that’s also not how it works in English. If I say “is the cat in the house?”, then the cat is what I’m asking you to look for, and the house is where I’m asking you to look - not the other way around.

If you mean (and thanks to @MRAB because otherwise I wouldn’t have been able to think of this possibility) “why am I able to use programmer_info directly like this, and I don’t have to choose an entry from languages?” - that’s what the loop was for. Every time through the loop, programmer will be one of the names ('jen', 'sarah' etc.), and programmer_info will be the corresponding dictionary, which has the 'specializes' entry. That’s the purpose of the code for programmer, programmer_info in languages.items():; and if you took that from the previous lesson code and didn’t properly understand it, you should go back and review.

If you mean something else, then I’m sorry but I simply can’t understand the thought process.

Thanks for this reply. The solution itself looks logical to me. The reason I didn’t know and wanted an explanation is because as someone new to programming I see a bunch of stuff that’s complex for me. For example a list comprehension looks like it has so much going on (this in that in this in that in this in that). I didn’t know a value could be on the left side. I like that this solution is close to how we’d say it in English.