how to merge a list of strings with numpy arrays stored in a list

I have a list of numpy arrays and a list of strings. I want to merge strings with the arrays. These are my arrays and strings:

arrs=[np.array([[1.,0.,0.], [1.,1.,1.]]), np.array([[4.,4.,4.], [2.,4.,0.]]),
       np.array([[9.,0.,9.], [9.,7.,5.]])]
names=['group_A', 'group_B', 'group_H']

First string in names should be added as the last column of first array in arrs. Second one also merges with second array and so on. Finally, I want to merge all the arrays ans get it as:

merged_arrs=[np.array([[1.,0.,0., 'group_A'], [1.,1.,1., 'group_A'],
                       [4.,4.,4., 'group_B'], [2.,4.,0., 'group_B'],
                       [9.,0.,9., 'group_H'], [9.,7.,5., 'group_H']])]

I have no idea how to concatenate these strings with arrays in python and do appreciate any help in advance.

Answer

How about we start with lists first, then convert each sub list into np.array later? I don’t know why you need np.array instead of using built-in lists, but setting dtype='object' in np.array allows you to store items with different types.

Example Code

import numpy as np

arrs = [np.array([[1., 0., 0.], [1., 1., 1.]]),
        np.array([[4., 4., 4.], [2., 4., 0.]]),
        np.array([[9., 0., 9.], [9., 7., 5.]])]

# Convert each sub array into list
arrs = [A.tolist() for A in arrs]

names = ['group_A', 'group_B', 'group_H']

for A, name in zip(arrs, names):
    for A_sub in A:
        A_sub.append(name)

# Convert each sub array into numpy array
merged_arrs = [np.array(A, dtype='object') for A in arrs]

print(merged_arrs)

# Outputs:
# [array([[1.0, 0.0, 0.0, 'group_A'], [1.0, 1.0, 1.0, 'group_A']], dtype=object), 
#  array([[4.0, 4.0, 4.0, 'group_B'], [2.0, 4.0, 0.0, 'group_B']], dtype=object), 
#  array([[9.0, 0.0, 9.0, 'group_H'], [9.0, 7.0, 5.0, 'group_H']], dtype=object)]

Reference

https://numpy.org/doc/stable/reference/arrays.dtypes.html#specifying-and-constructing-data-types