2

I produced the following code to loop keys through a dictionary

favourite_languages = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python'
    }

friends = ['sarah', 'phil']
for name in favourite_languages.keys():
    print(name.title())

if name in friends:
    print(" Hi " + name.title() + ", I see your favourite language is " +
          favourite_languages[name].title() + "!")

The output produced is

Jen
Sarah
Edward
Phil
 Hi Phil, I see your favourite language is Python!

I however, was expecting the following:

Jen
Sarah
 Hi Sarah, I see your favourite language is C!
Edward
Phil
 Hi Phil, I see your favourite language is Python!

Why does Sarah not come up in the output?

2
  • 7
    You need to move your if one indent level deeper. Commented Jul 12, 2017 at 12:00
  • Got it! @cᴏʟᴅsᴘᴇᴇᴅ submit as an answer and I'll accept it. Commented Jul 12, 2017 at 12:01

7 Answers 7

4

The solution is that you just have to move you if condition inside loop.

favourite_languages = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python'
    }

friends = ['sarah', 'phil']
for name in favourite_languages.keys():
    print(name.title())

    if name in friends:
        print(" Hi " + name.title() + ", I see your favourite language is " +
              favourite_languages[name].title() + "!")

Output:

Sarah
 Hi Sarah, I see your favourite language is C!
Edward
Jen
Phil
 Hi Phil, I see your favourite language is Python!
Sign up to request clarification or add additional context in comments.

2 Comments

How does this differ from the other solutions posted?
I'm writing an answer, After post the answer I was come to know that you have already given the answer. I have tried my best to give an answer.
3

You'll need to move your if statement into the loop. In its current state, it is outside, and so will have access to only the last value that name took on.

Additionally, I recommend you store your friends as a set, for quick and efficient lookup.

In [193]: favourite_languages = {
     ...:     'jen':'python',
     ...:     'sarah':'c',
     ...:     'edward':'ruby',
     ...:     'phil':'python'
     ...:     }
     ...: 
     ...: friends = {'sarah', 'phil'}
     ...: for name in favourite_languages.keys():
     ...:     print(name.title())
     ...: 
     ...:     if name in friends:
     ...:         print(" Hi " + name.title() + ", I see your favourite language is " +
     ...:               favourite_languages[name].title() + "!") 
     ...:           
Phil
 Hi Phil, I see your favourite language is Python!
Jen
Edward
Sarah
 Hi Sarah, I see your favourite language is C!

Sets have constant time (O(1)) lookup, while lists are linear (O(n)). It may not matter much for your particular use case, but sets really shine with large amounts of data.

As a further improvement, you don't need to call dict.keys(), since that creates a list of keys for the loop to iterate over, which is a little wasteful. The same thing may be achieved with:

for name in favourite_languages:

This is simpler and more efficient.

1 Comment

@user3416724 Also note that favourite_languages.keys() can be replaced with favourite_languages to the same effect. Cheers.
2

The name variable in your if statement is set to the last value of friends after the loop, and the if statement is only executed once.

I think you want to indent the if statement (and print) so that it's executed in each iteration of the loop

Comments

2

You're not looping over the name variable.

When your code reaches the if name in friends, the value stored in name is phil.

You could loop over the names again (which would produce a slightly different result), or add a indent to your code.

favourite_languages = {
'jen':'python',
'sarah':'c',
'edward':'ruby',
'phil':'python'
}

friends = ['sarah', 'phil']
for name in favourite_languages.keys():
    print(name.title())

    if name in friends:
        print(" Hi " + name.title() + ", I see your favourite language is " +
              favourite_languages[name].title() + "!")

Comments

1

Indent your if block one level more.

...
for name in favourite_languages.keys():
    print(name.title())
    if name in friends:
        print(" Hi " + name.title() + ", I see your favourite language is " +
              favourite_languages[name].title() + "!")

Comments

1

You need to indent if name in friends:

favourite_languages = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python'
    }

friends = ['sarah', 'phil']
for name in favourite_languages.keys():
    print(name.title())

    if name in friends:
        print(" Hi " + name.title() + ", I see your favourite language is " +
          favourite_languages[name].title() + "!")

Comments

1

Just move your if statement into your for loop.

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.