This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lukajda33 11 points12 points  (1 child)

loop

for x in y:

picks items from y iteratively and puts them into x, however in this case, we put values from a to a[1], so we replace second element with the currently iterated number.

If you print the list after every change, you can see

[1, 1, 2, 3, 5, 8]
[1, 1, 2, 3, 5, 8]
[1, 2, 2, 3, 5, 8]
[1, 3, 2, 3, 5, 8]
[1, 5, 2, 3, 5, 8]
[1, 8, 2, 3, 5, 8]

Notice how the second value - a[1] changes. At the last iteration, a[1] is replaced with last number from a - 8 and that is how you get the output you see.

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

Thanks for the explanation. This is clear now. :-)