Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding elements from one numpy array to another numpy array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading