I want to append each element of
[1,2]
to[[1], [2], [3]]
and as a consequence, the final array that I want is[[1,1], [1,2], [2,1], [2,2], [3,1], [3,2]]
But my code has a mistake I couldn’t recognize it yet, and the result of the python code below is
[[1, 1, 2], [1, 1, 2], [2, 1, 2], [2, 1, 2], [3, 1, 2], [3, 1, 2]]
The python code:
tor=[] arr=[1,2] arz=[[1], [2], [3]] each=0 while each<len(arz): eleman=arz[each] index=0 while index < len(arr): k=arr[index] eleman=arz[each] eleman.append(k) tor.append(eleman) index = index+1 each=each+1
Advertisement
Answer
it would be eleman=arz[each].copy()
as lists are mutable so every time you change an element in the original list it will get reflected in the resultant array