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 numpy arrays to specific indices of a list

I have a list of numpy arrays and want to add two more arrays as the first (0) and last (-1) indices of the list. These are my list and arrays:

import numpy as np
first_arr = np.array([1,1,1])
last_arr = np.array([9,9,9])
middle_arr = [np.array ([4,4,4]), np.array ([6,6,6])]

Then, I want to have a complete_arr which is:

complete_arr = [np.array ([1,1,1]), np.array ([4,4,4]), np.array ([6,6,6]), np.array ([9,9,9])]

I tried the following method but it did not give me what I want:

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

insert_at = 0
middle_arr[insert_at:insert_at] = first_arr

I very much appreciate if anyone help me to do it in python.

>Solution :

You can use * (unpacking) operator:

first_arr = np.array([1,1,1])
last_arr = np.array([9,9,9])
middle_arr = [np.array ([4,4,4]), np.array ([6,6,6])]

complete_arr = [first_arr, *middle_arr, last_arr]
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