Why using a for loop here does not give the desired outcome

Screen Link: Link to the lesson: https://app.dataquest.io/m/331/python-data-analysis-basics/7/creating-an-artist-summary-function

My Code:

def artist_summary(name):
    for name in artist_freq:
        num_artworks= artist_freq[name]
        template= "There are {artwork} artworks by {artist_name} in the data set"
        output= template.format(artwork= num_artworks, artist_name= name)
        print(output)
        
artist_summary('Henri Matisse')

What I expected to happen:
I expected it to return only the value that I inserted in the function and I wanted it to print: There are 129 artworks by Henri Matisse in the data set.

What actually happened:
Instead, it looped (duh?) through the whole dictionary.

The way I understood the loop within a function is that it will replace the parameter of the argument where the argument is being mentioned inside the function. Therefore, in my case Henri Matisse will be placed also in for name in artist_freq giving the result for Henri Matisse in artist_freq. This prompted me to believe that the loop’s functionality will stop there since I gave it a specific variable to iterate.

I figured out by myself that if I eliminated the for loop I will get the right answer, but I am still in need for an answer on why was it necessary.

Is someone kind enough to explain this to me? Thank you very much for your time.

Hi vallentin.ciocirlan

The artist_freq is a dictionary that stores artist’s name as the key and number of artworks done by that artist as the corresponding value.
When you are not using a for loop in your code, artist_freq[name], is artist_freq['Henri Matisse'], which will fetch you number of artworks done by ‘Henri Matisse’.
When you are using a for loop for each and every iteration, the value of name will be different, all the artist names in the dictionary will be assigned to name variable in each and every iteration, the number of artworks for that artist will be found out and the print statement will print the formatted output.
For this problem, we are expected to find the value for a particular key in the dictionary.
Hence, artist_freq['name'] will do the needful, you don’t have to use a for loop and traverse all through the dictionary.

Hope its clear now?

1 Like

Hello @dash.debasmita

Thanks a lot for the answer. So the for will be going through the whole dictionary, even though it is part of a function which will be a specific parameter I assigned. Thus, it will return to me the whole dictionary no matter what, because this it what a for loop does.

Exactly. And in the for loop name will have a key value from the dict, not the one passed to the function parameter,

Here scope of name is local to the for loop, the one from the function

has no role to play in the for loop. Once you get outside the for loop, artist_summary(name) will be coming in to play.

1 Like

Hi @vallentin.ciocirlan

I think the confusion occurred to you because of the variable names you have given.

When you called the function artist_summary('Henri Matisse'), the value ‘Henri Matisse’ did pass to the argument name of the function you created. But then you have used the same variable name (name) for the for loop as well. Then the variable : name took the role of iterating variable and started accepting the values it received by iterating through the whole dictionary. Due to the local scope of iterating variable within the for loop name became the value it stored during each iteration and didn’t end up using the value you supplied ( ‘Henri Matisse’).

If artistic_freq dictionary is storing the number of artworks produced by artists, you don’t need to loop through the dictionary to find an artist and his number of works. You can simply get the value by using the key which is the name of the artist. Then it will give you the value connected with that key ie. the total number of artwork of that artist.

1 Like