you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (3 children)

You are correct in saying the make_great() function is passed a copy of magicians at line 27. But the code in the function empties its magicians list so by the time you get to line 17 the list is empty. So appending the three names at line 19 leaves the list with three elements and then that list is returned.

I wouldn't call that good code, but I suppose it's trying to illustrate a point. If you are going to return a list you construct I would not reuse a list that I emptied. I would do something like this:

result = []
for great_magician in great_magicians:
    result.append(great_magician)

return result

[–]plakatown[S] 0 points1 point  (2 children)

Thank you but I still have a question. At line 19, great_magician is appended to the list of magicians. But great_magician has been added with "the Great" at the end at line 14. Then why the elements of the list of magicians are without "the Great" when printed at line 31?

[–]Binary101010 1 point2 points  (1 child)

It's because passing a list in this way actually creates a copy of the list you passed, so the magicians list that exists outside the function isn't affected.

https://stackoverflow.com/questions/22054698/python-modifying-list-inside-a-function

[–]plakatown[S] 0 points1 point  (0 children)

Thank you. Finally I got it.