0

I want to make an list consist on subsequences of another list, for example:

input: array = [4, 1, 8, 2] output that i expect: [[4], [4,1], [4, 1, 8], [4, 1, 8, 2]]

but output is: [[4, 1, 8, 2], [4, 1, 8, 2], [4, 1, 8, 2], [4, 1, 8, 2]]

my code is:

num = [4, 1, 8, 2]
temp = []
sub = []
for item in num:
    temp.append(item)
    sub.append(temp)

append() must add an item to the end of the list, but here every time it adds the temp to the sub it changes the previous items as well, so if it's not a bug please help me to write the correct code.

nima
  • 1

0 Answers0