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. This may result in incorrect results for large integer values: >>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect
๐ŸŒ
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.
๐ŸŒ
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.
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.

๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 16, 2025 - import numpy as np # Create a 1D array with sequential numbers numbers = np.arange(12) # Creates [0, 1, 2, ..., 11] # Reshape it into a 2D array reshaped_array = numbers.reshape(4, 3) # You can also do this in one step direct_reshaped = np.arange(15).reshape(3, 5) print("Original 1D array:", numbers) print("\nReshaped to 2D (4ร—3):\n", reshaped_array) print("\nDirect reshape (3ร—5):\n", direct_reshaped)
๐ŸŒ
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 - For example, the following code creates a 2D NumPy array with 3 rows and 4 columns using the arange() and reshape() functions: import numpy as np # Create a sequence of numbers numbers = np.arange(12) # Reshape the sequence into a 2D array array = numbers.reshape(3, 4) # Print the array print(array)
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ quickstart.html
NumPy quickstart โ€” NumPy v2.5.dev0 Manual
>>> a = np.arange(6) # 1d array >>> print(a) [0 1 2 3 4 5] >>> >>> b = np.arange(12).reshape(4, 3) # 2d array >>> print(b) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] >>> >>> c = np.arange(24).reshape(2, 3, 4) # 3d array >>> print(c) [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] See below to get more details on reshape.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: arange() and linspace() to generate evenly spaced values | note.nkmk.me
February 2, 2024 - Like range(), np.arange() generates an ndarray with evenly spaced values according to the specified arguments:
๐ŸŒ
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, ...
๐ŸŒ
Our Coding Club
ourcodingclub.github.io โ€บ tutorials โ€บ numpy
Numbers in Python with NumPy
You can create 2D, 3D or any-D arrays, by creating a 1D array, and reshaping it. Try entering the code below in Python. a = np.arange(9.0).reshape(3,3) print(a.shape) a will be a 2D array of size 3x3. The rows will be [0., 1., 2.], [3., 4., 5.], [6., 7., 8.]. The print statement should return a tuple (3, 3).
๐ŸŒ
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. This may result in incorrect results for large integer values: >>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert 1D Array to 2D Array in Python (numpy.ndarray, list) | note.nkmk.me
May 15, 2023 - import numpy as np a = np.arange(6) print(a) # [0 1 2 3 4 5] print(a.reshape(2, 3)) # [[0 1 2] # [3 4 5]] print(a.reshape(-1, 3)) # [[0 1 2] # [3 4 5]] print(a.reshape(2, -1)) # [[0 1 2] # [3 4 5]]
๐ŸŒ
CodingNomads
codingnomads.com โ€บ arrays-in-numpy-ndarray
Arrays in NumPy: ndarray, np.empty, np.arange, np.linspace
There are a number of ways to create NumPy arrays, for example, from simply passing it a Python list (like we did above) to using automatic methods such as arange, linspace, etc. Let's go over some examples: # already did this a = np.array([1, 2, 3, 4, 5]) # now using floats b = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) print(b.dtype) # 2D array multi_list_a = np.array([[1, 2, 3], [4, 5, 6]]) print(multi_list_a) print() # 2D array but with floats and using tuples multi_list_b = np.array([(1.5, 2.1, 3), (4, 5, 6)]) print(multi_list_b)
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ quickstart.html
NumPy quickstart โ€” NumPy v2.4 Manual
>>> a = np.arange(6) # 1d array >>> print(a) [0 1 2 3 4 5] >>> >>> b = np.arange(12).reshape(4, 3) # 2d array >>> print(b) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] >>> >>> c = np.arange(24).reshape(2, 3, 4) # 3d array >>> print(c) [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15] [16 17 18 19] [20 21 22 23]]] See below to get more details on reshape.
๐ŸŒ
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. This may result in incorrect results for large integer values: >>> power = 40 >>> modulo = 10000 >>> x1 = [(n ** power) % modulo for n in range(8)] >>> x2 = [(n ** power) % modulo for n in np.arange(8)] >>> print(x1) [0, 1, 7776, 8801, 6176, 625, 6576, 4001] # correct >>> print(x2) [0, 1, 7776, 7185, 0, 5969, 4816, 3361] # incorrect