0

Let's say I have array 'a' like this

>>> a = np.array([[1,2], [3,4,5]])

>>> a.shape

(2,)
>>> 

Now I'd like to append the entire of an array like [6,7,8,9] to it as just one element and not four:

>>> b = np.append(a,[6,7,8,9])

>>> b.shape

(6,)
>>> 

But this is not what I want. What I do want is to have b having 3 elements and not 6 by stacking every element of the new array to a. I found a workaround this as below but what is the proper way to get this done?

>>> b = np.append(a,[0])

>>> b[2] = [6,7,8,9]

>>> b.shape

(3,)
>>> 
4
  • It's best to avoid np.append. It is not a list append clone, so don't expect the same behavior. It is a cover for np.concatenate, but first it will tweak the inputs (if you don't specify axis). Commented Jun 2, 2020 at 16:31
  • Your a is an object dtype array. Expect some differences in behavior compared to working with a normal numeric array. In a sense there isn't a proper way of adding an element (especially an array or list) to an object array. a is itself isn't proper. Commented Jun 2, 2020 at 16:35
  • Yes hpaulj, I tried first with np.concatenate and the same results. Commented Jun 2, 2020 at 16:36
  • 1
    Why are you using an array for this? You just made an array of lists. The lists are not very efficient in terms of bulk operations, and arrays are not good for appending. It's the worst of both worlds. Did you mean to have a list of array instead? Commented Jun 2, 2020 at 16:55

2 Answers 2

1

You shouldn't create numpy array with arrays of different sizes, because it's not how it works.

Result of first line, will be numpy array of 2 python lists. If you want to have arrays of different sizes then you should use python list(), not ndarray.

And then you will be able to use append method.

a = [[1, 2], [3, 4, 5]]
a.append([6, 7, 8, 9])
Sign up to request clarification or add additional context in comments.

Comments

1

Define 2 arrays:

In [19]: a = np.array([[1,2], [3,4,5]])                                         
In [20]: b = np.array([6,7,8,9])                                                
In [21]: a                                                                      
Out[21]: array([list([1, 2]), list([3, 4, 5])], dtype=object)

Since a is an array of lists, lets backtrack and define b as a list as well:

In [22]: b = [6,7,8,9]                                                          

An object array has few, if any advantages compared to a list. On big disadvantage is that it doesn't have an append method:

In [23]: a.tolist()                                                             
Out[23]: [[1, 2], [3, 4, 5]]
In [24]: alist = a.tolist()                                                     
In [25]: alist.append(b)                                                        
In [26]: np.array(alist)                                                        
Out[26]: array([list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9])], dtype=object)

While we can work out a way of making new array with 3 elements, I doubt if it will be any better than this list append.

When we try to concatenate, the b list is first turned into an array:

In [30]: np.concatenate([a,b])                                                  
Out[30]: array([list([1, 2]), list([3, 4, 5]), 6, 7, 8, 9], dtype=object)
In [31]: np.array(b)                                                            
Out[31]: array([6, 7, 8, 9])

So this joins the (2,) a with the (4,) b producing a (6,).

To do the concatenate that you want, we have to first wrap b in an similar object dtype array:

In [32]: c = np.array([None])                                                   
In [33]: c                                                                      
Out[33]: array([None], dtype=object)
In [34]: c[0]=b                                                                 
In [35]: c                                                                      
Out[35]: array([list([6, 7, 8, 9])], dtype=object)

c is a (1,) object dtype, which pairs with the (2,) a as desired:

In [36]: np.concatenate((a,c))                                                  
Out[36]: array([list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9])], dtype=object)

This is effectively the same as your appending [0]:

In [40]: d = np.concatenate([a, [None]])                                        
In [41]: d                                                                      
Out[41]: array([list([1, 2]), list([3, 4, 5]), None], dtype=object)
In [42]: d[-1] = b                                                              
In [43]: d                                                                      
Out[43]: array([list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9])], dtype=object)

One way or other you have to convert b into a single element array, not the 4 element Out[31]. concatenate joins arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.