How to build an array of all combinations of two NumPy arrays?
Last Updated :
25 Apr, 2025
Our task is to build an array containing all possible combinations of elements from two NumPy arrays. To achieve this, we can utilize the np.meshgrid() function, which creates coordinate grids from the arrays. By reshaping these grids, we can generate a 2D array that holds every combination of values from the two input arrays.
Syntax:
numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')
Examples of building an array of all combinations of two NumPy arrays
Example 1: Computing combinations of elements of Two NumPy arrays
This code demonstrates how to combine two 1D NumPy arrays using np.meshgrid() to create a 2D grid of coordinates and then reshape it into a 2D array.
Python
import numpy as np
a = np.array([1, 2])
b = np.array([4, 6])
print("Array-1")
print(a)
print("\nArray-2")
print(b)
res = np.array(np.meshgrid(a, b)).T.reshape(-1, 2)
print("\nCombine array:")
print(res)
OutputArray-1
[1 2]
Array-2
[4 6]
Combine array:
[[1 4]
[1 6]
[2 4]
[2 6]]
Explanation: Two 1D arrays a
and b
are created. np.meshgrid(a, b)
creates coordinate grids from the arrays a
and b
. The result is reshaped into a 2D array with pairs of coordinates using .T.reshape(-1, 2)
. The combined array of all coordinate pairs is printed.
Example 2: Computing combinations of elements of Three NumPy arrays
This code demonstrates how to combine three 1D NumPy arrays using np.meshgrid() to create a 3D grid of coordinates and then reshape it into a 2D array of coordinate triples.
Python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 6, 4])
c = np.array([3, 6])
print("Array-1")
print(a)
print("Array-2")
print(b)
print("Array-3")
print(c)
res = np.array(
np.meshgrid(a, b, c)).T.reshape(-1, 3)
print("\nCombine array:")
print(res)
Output
Array-1
[1 2 3]
Array-2
[4 6 4]
Array-3
[3 6]
Combine array:
[[1 4 3]
[1 6 3]
[1 4 3]
[2 4 3]
[2 6 3]
[2 4 3]
[3 4 3]
[3 6 3]
[3 4 3]
[1 4 6]
[1 6 6]
[1 4 6]
[2 4 6]
[2 6 6]
[2 4 6]
[3 4 6]
[3 6 6]
[3 4 6]]
Explanation: The code begins by creating three 1D NumPy arrays, a, b, and c. Using np.meshgrid(a, b, c), coordinate grids are generated from these arrays. The result is then reshaped into a 2D array of coordinate triples by applying .T.reshape(-1, 3).
Example 3: Computing combinations of elements of Four NumPy arrays
This code demonstrates how to combine four 1D NumPy arrays into all possible combinations of their elements using np.meshgrid(). It creates a 4D coordinate grid and then reshapes the grid into a 2D array of coordinate quadruples.
Python
import numpy as np
a = np.array([50, 21])
b = np.array([4, 4])
c = np.array([1, 10])
d = np.array([7, 14])
print("Array-1")
print(a)
print("Array-2")
print(b)
print("Array-3")
print(c)
print("Array-4")
print(d)
res = np.array(np.meshgrid(a, b, c, d)).T.reshape(-1, 4)
print("\nCombine array:")
print(res)
Output
Array-1
[50 21]
Array-2
[4 4]
Array-3
[ 1 10]
Array-4
[ 7 14]
Combine array:
[[50 4 1 7]
[50 4 1 7]
[21 4 1 7]
[21 4 1 7]
[50 4 10 7]
[50 4 10 7]
[21 4 10 7]
[21 4 10 7]
[50 4 1 14]
[50 4 1 14]
[21 4 1 14]
[21 4 1 14]
[50 4 10 14]
[50 4 10 14]
[21 4 10 14]
[21 4 10 14]]
Explanation: The code creates four 1D arrays (a, b, c, d) and uses np.meshgrid() to generate a 4D coordinate grid. It then reshapes the grid into a 2D array of coordinate quadruples using .T.reshape(-1, 4). The result is a 2D array containing all possible combinations of values from the four arrays.
Similar Reads
How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array.
2 min read
Combining a one and a two-dimensional NumPy Array Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer(), which provides this facility. Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0) Example 1
2 min read
How to Concatenate two 2-dimensional NumPy Arrays? Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it: # import numpy package import numpy as np Then two 2D arrays
4 min read
How to convert 1-D arrays as columns into a 2-D array in Python? Let's see a program to convert 1-D arrays as columns into a 2-D array using NumPy library in Python. So, for solving this we are using numpy.column_stack() function of NumPy. This function takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. Syntax : numpy.column_stac
1 min read
Generate a matrix product of two NumPy arrays We can multiply two matrices with the function np.matmul(a,b). When we multiply two arrays of order (m*n) and  (p*q ) in order to obtained matrix product then its output contains m rows and q columns where n is n==p is a necessary condition. Syntax: numpy.matmul(x1, x2, /, out=None, *, casting='same
2 min read
Element-wise concatenation of two NumPy arrays of string In this article, we will discuss how to concatenate element-wise two arrays of string Example : Input : A = ['Akash', 'Ayush', 'Diksha', 'Radhika'] B = ['Kumar', 'Sharma', 'Tewari', 'Pegowal'] Output : A + B = [AkashKumar, AyushSharma, 'DikshTewari', 'RadhikaPegowal'] We will be using the numpy.cha
2 min read
Ways to Convert a Python Dictionary to a NumPy Array The task of converting a dictionary to a NumPy array involves transforming the dictionaryâs key-value pairs into a format suitable for NumPy. In Python, there are different ways to achieve this conversion, depending on the structure and organization of the resulting array.For example, consider a dic
3 min read
How to append two NumPy Arrays? Prerequisites: Numpy Two arrays in python can be appended in multiple ways and all possible ones are discussed below. Method 1: Using append() method This method is used to Append values to the end of an array. Syntax : numpy.append(array, values, axis = None) Parameters : array: [array_like]Input a
4 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read
NumPy | Multiply 2D Array to 1D Array Given two NumPy arrays, the task is to multiply a 2D array with a 1D array, each row corresponding to one element in NumPy. You can follow these methods to multiply a 1D array into a 2D array in NumPy: Using np.newaxis()Using axis as noneUsing transpose()Let's understand them better with Python prog
2 min read