Adding elements from one numpy array to another numpy array

Question:

Good morning,

I am trying to add elements from one numpy array to the other.

Jack = ["red", "blue", "yellow"]

Louise = ["orange", "green", "purple"] 

When I do the following line, it adds the entire array instead of the elements.

Jack.append(Louise)

Out:

[["red", "blue", "yellow"],["orange", "green", "purple"]]

What I want to obtain in the end is:

["red", "blue", "yellow", "orange", "green", "purple]
Asked By: Jasmine Scott

||

Answers:

If you want to do it in numpy you can use numy.concatenate with axis=0 and if you want in list you can use list_1 + list_2.

Jack = np.array(["red", "blue", "yellow"])
Louise = np.array(["orange", "green", "purple"] )
out = np.concatenate((Jack, Louise), axis=0)
print(out)
# ['red' 'blue' 'yellow' 'orange' 'green' 'purple']

>>> Jack = ["red", "blue", "yellow"]
>>> Louise = ["orange", "green", "purple"] 
>>> Jack + Louise
['red', 'blue', 'yellow', 'orange', 'green', 'purple']
Answered By: I'mahdi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.