0
import numpy as np

x = np.array([1,2,3])
xlist = []
Xlist = []
for i in range(1):
    xlist.append(x**(i+1))
    Xlist.append(xlist)
xlist.append(x**2)
Xlist.append(xlist)
Xlist

However, the output is

[[array([1, 2, 3]), array([1, 4, 9])], [array([1, 2, 3]), array([1, 4, 9])]]

However, I don't know why the result isn't the expression below, and I want the result below, how to achieve that?

[[array([1, 2, 3])], [array([1, 2, 3]), array([1, 4, 9])]]

1 Answers1

0

you need to create a copy of the list first then append the sqaured array.

new_list = xlist+x**(i+1)
xlist.append(new_list)

your problem is occuring because np.array when appended are passed as reference so if you change it outside the list, the value will change in the list as well.