You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:

import numpy as np
X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]

For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:

X, Y = np.mgrid[-5:5:21j, -5:5:21j]

You can then create your pairs as:

xy = np.vstack((X.flatten(), Y.flatten())).T

As @ali_m suggested, this can all be done in one line:

xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T

Best of luck!

Answer from farenorth on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.4 Manual
The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.1 Manual
The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers.
๐ŸŒ
DataCamp
campus.datacamp.com โ€บ courses โ€บ introduction-to-python-for-finance โ€บ arrays-in-python
2D arrays and functions | Python
Another useful numpy function is arange(). You can use arange() to create an array of continuous numbers as shown here. The function arange() creates a numeric array given a start, end, and an optional step argument. Thus, arange with inputs 1 and 13 would create an array consisting of numbers ...
๐ŸŒ
Real Python
realpython.com โ€บ how-to-use-numpy-arange
NumPy arange(): How to Use np.arange() โ€“ Real Python
July 31, 2023 - In this step-by-step tutorial, you'll learn how to use the NumPy arange() function, which is one of the routines for array creation based on numerical ranges. np.arange() returns arrays with evenly spaced values.
Top answer
1 of 4
5

To do the first one with numpy:

>>> a = np.arange(11)
>>> a[:,None]+a
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10],  
  [ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],  
  [ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],  
  [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13],  
  [ 4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14],  
  [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15],  
  [ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16],  
  [ 7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17],  
  [ 8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18],  
  [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],  
  [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])  

For the second array, @Divakar has a good approach. Maybe a bit simpler syntax to do this:

>>> (a%a[:,None])==0
array([[ True,  True,  True,  True,  True,  True,  True,  True,  True, True,  True],
   [ True,  True,  True,  True,  True,  True,  True,  True,  True, True,  True],
   [ True, False,  True, False,  True, False,  True, False,  True, False,  True],
   [ True, False, False,  True, False, False,  True, False, False, True, False],
   [ True, False, False, False,  True, False, False, False,  True, False, False],
   [ True, False, False, False, False,  True, False, False, False, False,  True],
   [ True, False, False, False, False, False,  True, False, False, False, False],
   [ True, False, False, False, False, False, False,  True, False, False, False],
   [ True, False, False, False, False, False, False, False,  True, False, False],
   [ True, False, False, False, False, False, False, False, False, True, False],
   [ True, False, False, False, False, False, False, False, False, False,  True]], dtype=bool)
2 of 4
4

Per your first question:

np.add(*np.indices((nrow, ncol)))

For nrow=5, ncol=6 you get

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

This method doesn't use the numpy.arange function, though I find it more readable. Moreover, it supports cases when nrow != ncol.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: arange() and linspace() to generate evenly spaced values | note.nkmk.me
February 2, 2024 - In NumPy, the np.arange() and np.linspace() functions generate an array (ndarray) of evenly spaced values. You can specify the interval in np.arange() and the number of values in np.linspace(). numpy. ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 16, 2025 - For arrays with sequential values, numpy.arange() combined with reshape() offers an efficient approach.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.creation.html
Array creation โ€” NumPy v2.5.dev0 Manual
The advantage of this creation function is that you guarantee the number of elements and the starting and end point. The previous arange(start, stop, step) will not include the value stop. The 2D array creation functions e.g.
๐ŸŒ
Finxter
blog.finxter.com โ€บ numpy-arange
NumPy arange(): A Simple Illustrated Guide โ€“ Be on the Right Side of Change
NumPy is mostly about multi-dimensional matrices. It is common to create a 1D NumPy array with the NumPy arange function and to transform it immediately into a 2D array using the np.reshape() function.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ basic โ€บ numpy-basic-exercise-46.php
NumPy: Create a two-dimensional array of specified format - w3resource
print(np.arange(1, 151).reshape(15, -1)): This statement creates a NumPy array with integers from 1 to 150. It then reshapes it into a matrix with 15 rows, and the number of columns is inferred automatically (indicated by -1). In this case, the inferred number of columns is 10. A copy of the results is printed. For more Practice: Solve these Related Problems: Generate a 2D array of shape (5,5) with a specific numeric pattern using list comprehensions.
๐ŸŒ
Medium
medium.com โ€บ @bouimouass.o โ€บ what-does-a-numpy-2d-array-look-like-in-python-38752ccde895
What does a numpy 2D array look like in python? | by Omar | Medium
July 23, 2023 - You can also create a 2D NumPy array using the arange() function. The arange() function creates a sequence of numbers, and you can use the reshape() function to reshape the sequence into a 2D array.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ quickstart.html
NumPy quickstart โ€” NumPy v2.5.dev0 Manual
To create sequences of numbers, NumPy provides the arange function which is analogous to the Python built-in range, but returns an array.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ is-there-a-multi-dimensional-version-of-arrange-linspace-in-numpy.aspx
Python - Is there a multi-dimensional version of arange/linspace in numpy?
October 9, 2023 - # Import numpy import numpy as np # Using linspace method res = np.linspace(2.0, 3.0, num=5) # Display result print("Result:\n",res,"\n")
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-arrange-in-python
numpy.arange() in Python - GeeksforGeeks
January 24, 2025 - numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.ma.arange.html
numpy.ma.arange โ€” NumPy v2.0 Manual
The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_reshape.asp
NumPy Array Reshaping
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) newarr = arr.reshape(2, 3, 2) print(newarr) Try it Yourself ยป ยท Yes, as long as the elements required for reshaping are equal in both shapes. We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.5.dev0 Manual
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.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.arange.html
numpy.arange โ€” NumPy v2.5.dev0 Manual
The built-in range generates Python built-in integers that have arbitrary size, while numpy.arange produces numpy.int32 or numpy.int64 numbers.