0

I have a name list, and I want to construct a new list of the same type of data inside the list. How can I do it? What if it is a numpy array?

x=[1,2,3,4]
newx=[x,5,6]
[[1, 2, 3, 4], 5, 6]

#numpy
y=np.array([0,1,2,3,4])
newy=[y,5,6]
[array([0, 1, 2, 3, 4]), 5, 6]

Desired output

[1, 2, 3, 4, 5, 6]
MLE
  • 1,033
  • 1
  • 11
  • 30

1 Answers1

1

For mere Python lists you do:

x = [1, 2, 3, 4]
x += [5, 6]
>>> [1, 2, 3, 4, 5, 6]

and for numpy-arrays:

x = np.array([1, 2, 3, 4])
x = np.concatenate((x, np.array([5, 6])))
>>> np.array([1, 2, 3, 4, 5, 6])

Mind the double parentheses in np.concatenate, since the arguments must be passed as a tuple.

rammelmueller
  • 1,092
  • 1
  • 14
  • 29