I was trying some questions from hackerrank and came across this question https://www.hackerrank.com/challenges/list-comprehensions/problem
I tried this solution
This line clears the list by deleting all its items. The list stays the same variable (same object), only its contents is modified:
SL[:] = []
Later in the code is this line (in a loop):
L.append(SL)
But SL is the same variable each time. All items in L
refer to the same list SL
. So when SL
is modified, all items in L
refer to the new contents of SL
.
This can be fixed by creating a new list for each result.