AlexT
AlexT

Reputation: 609

Python - How to concatenate more than arrays lists in python?

I have many arrays, let's say

X = np.array([1, 2, 3, 4])
Y = np.array([5, 6, 7, 8])
Z = np.array([9, 10, 11, 12])

I want to concatenate them all so I have

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Here is what I've tried

arr = X + Y + Z

this doesn't work, here is my result

>>> print(arr)
[15, 18, 21, 24]

Just the sum of each element at an index i. Any suggestions?

Upvotes: 0

Views: 36

Answers (2)

bunbun
bunbun

Reputation: 2676

Use np.concatenate to combine np arrays:

import numpy as np 
X = np.array([1, 2, 3, 4])
Y = np.array([5, 6, 7, 8])
Z = np.array([9, 10, 11, 12])

print np.concatenate((X,Y,Z), axis=0)

Upvotes: 0

R.A.Munna
R.A.Munna

Reputation: 1709

you can use np.concatenate()

a = np.concatenate((X, Y, Z))
print(a)
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Upvotes: 1

Related Questions