Q: How to link elements inside a 2-D array with another list based on the index in python? The elements inside each array in the sample_array represents the index. How do I change these elements such that the results is similar to final_array?
sample_array = [np.array([5, 3, 2]), np.array([4, 0, 1, 6])
sample_list = [element_0, element_1, element_2, element_3, element_4, element_5, element_6]
final_array = [np.array([element_5, element_3, element_2]), np.array([element_4, element_0, element_1, element_6])


A: You can change the list to a numpy array and then use your indices:
sample_array = [np.array([5, 3, 2]), np.array([4, 0, 1, 6])]
sample_list = np.array(['element_0', 'element_1', 'element_2', 'element_3', 'element_4', 'element_5', 'element_6'])

sample_list = np.array(sample_list)

print([sample_list[sa] for sa in sample_array])
[array(['element_5', 'element_3', 'element_2'], dtype='<U9'),
 array(['element_4', 'element_0', 'element_1', 'element_6'], dtype='<U9')]


A: You can do this with the following:
np.array([[sample_list[i] for i in row] for row in sample_array])

See np.array's documentation for more details.

A: Another idea would be to marry the list comprehension and itemgetter. We need to use itemgetter instead of a normal slice operator since we want to extract elements from a list with arbitrary indices, as per your input:
# sample inputs
In [18]: sample_arr = [np.array([5, 3, 2]), np.array([4, 0, 1, 6])] 
In [19]: sample_list = [10, 11, 12, 13, 14, 15, 16]  

In [20]: from operator import itemgetter

We use a single list comprehension where we iterate over the items in the sample_arr list and use itemgetter for slicing. That would return a callable, to which we pass the list from which the elements need to be picked up (i.e. sample_list). Finally, we need to wrap the result in np.array() since you need a list of arrays, similar to the input.
final_array = [np.array(itemgetter(*arr.tolist())(sample_list)) for arr in sample_arr]  

In [22]: final_array 
Out[22]: [array([15, 13, 12]), array([14, 10, 11, 16])]

