Hi, I think the cause of the confusion is that it printed out the entire list every time you added an ingredient (called a function). In a real application much of this would be done under the hood but I think they are trying to demonstrate the basics of how lists work on a fundamental level.
Your output, three lists, was probably confusing because it looked like this :
[[‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’]]
[[‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’]]
[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]
The reason the output looks like this in this example is because every time you called a function it iterated through the list and performed the algorithm in the function you called including the output. When you ran all three you got the output of all three.
So when you ran:
add_spinach(on_the_griddle)
You called the add_spinach function and used the list “on_the_griddle” as an argument. Since your argument is a list, the function iterated through every item on the list and added spinach, giving you an output of the entire list:
[[‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’]]
When you called add_mushrooms, it did the exact same thing, iterating through the list and adding the sting ‘mushrooms’, giving you the output of the entire list:
[[‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’]]
Finally you added cheese by calling add_cheese and now you have the output of the list with all ingredients.
[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]
The point of the application was to generate this list. In a real world applications adding cheese or spinach would be given as a choice, you would add all the ingredients you needed and get the final output, and though the lists would be changed you would only see the final product, which in this case is:
[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]
The other printed lists are from the two previous functions you called and ran. Normally , even if those lists were changed, you would not see them printed. Here they were printed to show what is happening in the background.
As a side note, adding print statements to functions, even when they are not needed, helps you debug or just better understand what your code is doing at every step, as it did in this one.
I know this was a long post, I promise these concepts are important even if they seem basic. I hope it helped!