Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Numpy exercise
  • pandas
  • Matplotlib
  • Data visulisation
  • EDA
  • Machin Learning
  • Deep Learning
  • NLP
  • Data science
  • ML Tutorial
  • Computer Vision
  • ML project
Open In App
Next Article:
How to build an array of all combinations of two NumPy arrays?
Next article icon

How to build an array of all combinations of two NumPy arrays?

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Output
Array-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.


Next Article
How to build an array of all combinations of two NumPy arrays?

V

vanshgaur14866
Improve
Article Tags :
  • Python
  • Python-numpy
  • Python numpy-arrayCreation
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences