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.
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.
This produces:
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.
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.
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.
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.
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.
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
xandycreates every pair(x_i, y_j). - Use a list comprehension or
itertools.productfor plain Python. - Use NumPy with
meshgridandcolumn_stackwhen 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
- case-insensitive list sorting, without lowercasing the result?
- Case insensitive string as HashMap key
- CassandraThe stack size specified is too small, Specify at least 228k
- Casting AnyObject to Dictionary in swift
- Case insensitive 'in
- Case insensitive regular expression without re.compile?
- Caterpillars and Leaves. Can we do better than Onc?
- celery - call function on task done

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 courseTrack 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.