Cartesian Product
2D Arrays
Array Manipulation
Python
Data Structures

Cartesian product of x and y array points into single array of 2D points

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

The Cartesian product of two arrays x and y means generating every possible pair (x_i, y_j). In Python, the best implementation depends on what output you need: a list of tuples, a NumPy array of shape n x 2, or a grid for vectorized numerical work.

The Basic Python Solution

For plain Python, a list comprehension is the clearest way to create all 2D points.

python
1x = [1, 2]
2y = [3, 4, 5]
3
4points = [(xv, yv) for xv in x for yv in y]
5print(points)

This produces:

python
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)]

The order is deterministic: for each value in x, pair it with every value in y.

Using itertools.product

If you want a standard-library solution built exactly for Cartesian products, use itertools.product.

python
1from itertools import product
2
3x = [1, 2]
4y = [3, 4, 5]
5
6points = list(product(x, y))
7print(points)

This is often the most direct expression of intent because the function name matches the operation exactly.

Returning a NumPy Array of 2D Points

If you need a single numeric array for later vectorized computation, convert the Cartesian pairs into a NumPy array.

python
1import numpy as np
2from itertools import product
3
4x = np.array([1, 2])
5y = np.array([3, 4, 5])
6
7points = np.array(list(product(x, y)))
8print(points)
9print(points.shape)

Now the shape is (6, 2), which is often what downstream numerical code expects.

NumPy Approach with meshgrid

For larger numeric workflows, numpy.meshgrid is a natural tool because it creates coordinate grids efficiently.

python
1import numpy as np
2
3x = np.array([1, 2])
4y = np.array([3, 4, 5])
5
6xx, yy = np.meshgrid(x, y, indexing="ij")
7points = np.column_stack((xx.ravel(), yy.ravel()))
8
9print(points)

This gives the same pairs as the pure Python version, but it integrates better with NumPy-heavy code.

Choosing the Right Shape

There are two common output shapes:

  • list of tuples such as [(1, 3), (1, 4)]
  • array of shape (n, 2) such as [[1, 3], [1, 4]]

Use list-of-tuples when:

  • you are doing general Python processing
  • points are not purely numeric
  • readability matters more than vectorized performance

Use a NumPy n x 2 array when:

  • downstream code expects matrix-like data
  • you plan to run vectorized math
  • you want compact numeric storage

Performance Notes

Any Cartesian product of lengths m and n creates m * n pairs. That means memory grows with the number of output points, not just the size of the inputs.

If the output is large and you only need to stream through it once, prefer a generator.

python
1from itertools import product
2
3x = range(1000000)
4y = [0, 1]
5
6for point in product(x, y):
7    # process point without materializing the full result
8    pass

This avoids building a giant list in memory.

Example: Cartesian Points for Plotting

Here is a practical example that prepares points for plotting or simulation.

python
1import numpy as np
2
3x = np.array([0.0, 0.5, 1.0])
4y = np.array([10.0, 20.0])
5
6xx, yy = np.meshgrid(x, y, indexing="ij")
7points = np.column_stack((xx.ravel(), yy.ravel()))
8
9for point in points:
10    print(point)

This pattern is common in parameter sweeps, grid sampling, and geometric preprocessing.

Common Pitfalls

The most common mistake is expecting the result size to be m + n instead of m * n. Another is choosing a NumPy-heavy solution when a simple list of tuples would be clearer and easier to debug. Teams also sometimes confuse meshgrid output shape and end up with separate grids when they actually wanted a flat list of 2D points. Finally, materializing a huge Cartesian product unnecessarily can create avoidable memory pressure.

Summary

  • The Cartesian product of x and y creates every pair (x_i, y_j).
  • Use a list comprehension or itertools.product for plain Python.
  • Use NumPy with meshgrid and column_stack when you need numeric array output.
  • Choose the output shape based on downstream consumers.
  • Be mindful that the number of points grows as len(x) * len(y).

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms