Concatetate a list to a numpy array

This is a simple question. I am trying to concatenate a list to a numpy nd array, but I cannot figure out how to do it.

import numpy as np
t = list(range(2))
a = np.random.rand(2,4)

t = np.array(t)
result = np.concatenate(t,a)

>> ValueError: all the input arrays must have same number of dimensions

Upvotes: 0

Views: 39

Answers (1)

jpp
jpp

Reputation: 164843

You can concatenate an array of shape (2,) and an array of shape (2, 4) like this:

import numpy as np

t = list(range(2))
a = np.random.rand(2,4)
t = np.array(t)

result = np.hstack((t[:, None],a))

# array([[ 0.        ,  0.54789168,  0.60478776,  0.03923133,  0.51515207],
#        [ 1.        ,  0.52476824,  0.030079  ,  0.95157973,  0.99690973]])

Upvotes: 1

Related Questions