You want to reshape the array.

CopyB = np.reshape(A, (-1, 2))

where -1 infers the size of the new dimension from the size of the input array.

Answer from Matt Ball on Stack Overflow
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
You can use np.newaxis and np.expand_dims to increase the dimensions of your existing array. Using np.newaxis will increase the dimensions of your array by one dimension when used once. This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-1d-array-to-a-2d-numpy-array
Convert a 1D array to a 2D Numpy array - GeeksforGeeks
July 23, 2025 - You can divide the number of elements in your array by ncols. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # np.reshape(A,(-1,ncols)) B = np.reshape(arr, (-1, 2)) print('2D Numpy array: \n', B)
🌐
Medium
medium.com › @whyamit404 › how-to-reshape-1d-to-2d-in-numpy-f861f3cf73e5
How to Reshape 1D to 2D in NumPy? | by whyamit404 | Medium
February 26, 2025 - Here, 2 × 3 = 6, which matches the original array size. Tip: If the numbers don’t add up, NumPy will throw an error. (We’ll cover that in the FAQs.) 2.2 Reshaping with -1 (Automatic Dimension Calculation) This might surprise you: You don’t always need to know both dimensions! NumPy is smart enough to figure it out for you.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Convert 1D Array to 2D Array in Python (numpy.ndarray, list) | note.nkmk.me
May 15, 2023 - l = [0, 1, 2, 3, 4, 5] print(np.array(l).reshape(-1, 3).tolist()) # [[0, 1, 2], [3, 4, 5]] print(np.array(l).reshape(3, -1).tolist()) # [[0, 1], [2, 3], [4, 5]] ... See the following article on how to convert numpy.ndarray and list to each other. ... If NumPy is not available, you can still achieve the transformation using list comprehensions, range(), and slices. ... def convert_1d_to_2d(l, cols): return [l[i:i + cols] for i in range(0, len(l), cols)] l = [0, 1, 2, 3, 4, 5] print(convert_1d_to_2d(l, 2)) # [[0, 1], [2, 3], [4, 5]] print(convert_1d_to_2d(l, 3)) # [[0, 1, 2], [3, 4, 5]] print(convert_1d_to_2d(l, 4)) # [[0, 1, 2, 3], [4, 5]]
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_reshape.asp
NumPy Array Reshaping
Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise an error): import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(3, 3) print(newarr) Try it Yourself »
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_insert_axes_to_an_array.htm
NumPy - Insert Axes to an Array
By indexing with "None" or "np.newaxis", you can expand a 1D array to 2D or 3D, or adjust the shape as needed. In the example below, we are transforming a 1D NumPy array into a 2D row vector by adding a new axis with np.newaxis indexing −
🌐
Programiz
programiz.com › python-programming › numpy › array-reshaping
NumPy Array Reshaping (With Examples)
We use the reshape() function to reshape a 1D array into a 2D array. For example, import numpy as np array1 = np.array([1, 3, 5, 7, 2, 4, 6, 8]) # reshape a 1D array into a 2D array # with 2 rows and 4 columns result = np.reshape(array1, (2, 4)) print(result)
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2487741 › python-numpy-function-to-resize-a-1d-array-to-2d-array-with-fixed-noof-columns-and-no-restrictions-to-noof-rows
Python Numpy function to resize a 1D array to 2D array with fixed no.of columns and no restrictions to no.of rows | Sololearn: Learn to code for FREE!
September 9, 2020 - My array, `ary = [0, 1, 2, 3, 4 , 5, 6, 7, 8, 9]` Required output, ``` ary = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 0, 0]] ``` I know that if I use `ary.resize(3,4)` I will get the answer, but the size of input array varies, so I can't always specify the no.of rows. I want something like `ary.resize(3,n)` ?? ... Try this: import math import numpy as np ary = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) COLUMN = 3 row = math.ceil(ary.size / COLUMN) ary.resize(row, COLUMN) print(ary)
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_expand_dims.htm
Numpy expand_dims() Function
Following is the basic example of Numpy expand_dims() function which adds a new axis at the beginning of the 1D array by converting it into a 2D array with shape (1, 3) −
Top answer
1 of 9
73

The shortest in terms of lines of code i can think of is for the first question.

>>> import numpy as np
>>> p = np.array([[1,2],[3,4]])

>>> p = np.append(p, [[5,6]], 0)
>>> p = np.append(p, [[7],[8],[9]],1)

>>> p
array([[1, 2, 7],
   [3, 4, 8],
   [5, 6, 9]])

And the for the second question

    p = np.array(range(20))
>>> p.shape = (4,5)
>>> p
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> n = 2
>>> p = np.append(p[:n],p[n+1:],0)
>>> p = np.append(p[...,:n],p[...,n+1:],1)
>>> p
array([[ 0,  1,  3,  4],
       [ 5,  6,  8,  9],
       [15, 16, 18, 19]])
2 of 9
46

A useful alternative answer to the first question, using the examples from tomeedee’s answer, would be to use numpy’s vstack and column_stack methods:

Given a matrix p,

>>> import numpy as np
>>> p = np.array([ [1,2] , [3,4] ])

an augmented matrix can be generated by:

>>> p = np.vstack( [ p , [5 , 6] ] )
>>> p = np.column_stack( [ p , [ 7 , 8 , 9 ] ] )
>>> p
array([[1, 2, 7],
       [3, 4, 8],
       [5, 6, 9]])

These methods may be convenient in practice than np.append() as they allow 1D arrays to be appended to a matrix without any modification, in contrast to the following scenario:

>>> p = np.array([ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
>>> p = np.append( p , [ 7 , 8 , 9 ] , 1 )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 3234, in append
    return concatenate((arr, values), axis=axis)
ValueError: arrays must have same number of dimensions

In answer to the second question, a nice way to remove rows and columns is to use logical array indexing as follows:

Given a matrix p,

>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )

suppose we want to remove row 1 and column 2:

>>> r , c = 1 , 2
>>> p = p [ np.arange( p.shape[0] ) != r , : ] 
>>> p = p [ : , np.arange( p.shape[1] ) != c ]
>>> p
array([[ 0,  1,  3,  4],
       [10, 11, 13, 14],
       [15, 16, 18, 19]])

Note - for reformed Matlab users - if you wanted to do these in a one-liner you need to index twice:

>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )    
>>> p = p [ np.arange( p.shape[0] ) != r , : ] [ : , np.arange( p.shape[1] ) != c ]

This technique can also be extended to remove sets of rows and columns, so if we wanted to remove rows 0 & 2 and columns 1, 2 & 3 we could use numpy's setdiff1d function to generate the desired logical index:

>>> p = np.arange( 20 ).reshape( ( 4 , 5 ) )
>>> r = [ 0 , 2 ]
>>> c = [ 1 , 2 , 3 ]
>>> p = p [ np.setdiff1d( np.arange( p.shape[0] ), r ) , : ] 
>>> p = p [ : , np.setdiff1d( np.arange( p.shape[1] ) , c ) ]
>>> p
array([[ 5,  9],
       [15, 19]])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.expand_dims.html
numpy.expand_dims — NumPy v2.4 Manual
atleast_1d, atleast_2d, atleast_3d · Examples · Try it in your browser! >>> import numpy as np >>> x = np.array([1, 2]) >>> x.shape (2,) The following is equivalent to x[np.newaxis, :] or x[np.newaxis]: >>> y = np.expand_dims(x, axis=0) >>> y array([[1, 2]]) >>> y.shape (1, 2) The following is equivalent to x[:, np.newaxis]: >>> y = np.expand_dims(x, axis=1) >>> y array([[1], [2]]) >>> y.shape (2, 1) axis may also be a tuple: >>> y = np.expand_dims(x, axis=(0, 1)) >>> y array([[[1, 2]]]) >>> y = np.expand_dims(x, axis=(2, 0)) >>> y array([[[1], [2]]]) Note that some examples may use None instead of np.newaxis.
🌐
IncludeHelp
includehelp.com › python › simplest-way-to-extend-a-numpy-array-in-2-dimensions.aspx
Python - Simplest way to extend a NumPy array in 2 dimensions
February 11, 2023 - For this purpose, we can either use numpy.append() or numpy.vstack() and numpy.column_stack() methods to extend this multi-dimensional array.
🌐
w3resource
w3resource.com › numpy › manipulation › expand-dims.php
NumPy: numpy.expand_dims() function - w3resource
April 23, 2026 - For example, if we have a 1-dimensional ... array, we can use expand_dims() function to add a new axis to the 1-dimensional array to make it a 2-dimensional array before performing the multiplication....
🌐
GeeksforGeeks
geeksforgeeks.org › python-flatten-a-2d-numpy-array-into-1d-array
Python | Flatten a 2d numpy array into 1d array - GeeksforGeeks
February 3, 2023 - Converting a 2D float array to a 2D integer array in NumPy is a straightforward process using the astype() method. This conversion can be useful in various data analysis and scientific computing tasks where integer data types are required or ...