Numpy roll Explained With Examples in Python

Hello geeks and welcome in this article, we will cover NumPy roll(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But at first, let us try to analyze the function through its definition.

Suppose you have an array having n number of elements in it. Then suddenly, a situation arrives where you are forced to shift each element by m position. In such cases, the function NumPy roll() comes handy. Also, it doesn’t change the shape of the parent array. This will become a lot more clear to you as we move ahead in this article. In the next section, we will look at the syntax associated with this.


numpy.roll(ashift )

This is the general syntax for our function. In the next section, we will see different parameters associated with our function.


1. a:array_like

This parameter represents the input array on which the operation needs to be performed.

2. shift:int or tuples of int

This represents the number of places by which the elements of the array need to be shifted. If it is of the type tuple, then the axis must be of the same type. If an int while the axis is a tuple of int, then the same number of values are used for all given axis.

3. Axis: Int or tuples of int

Axis along which the elements are shifted. By default, the parent array is first flattened, following which the operation is performed then the original shape is restored.


As we are done with all the theory portion related to NumPy roll(). This section will be looking at how this function works and how it helps us achieve our desired output. We will start with an elementary level example and gradually move our way to more complicated examples.

1. Basic example for Numpy roll()

import numpy as ppool


[[4 5 1]
 [2 3 6]]

In the above example, we have first imported the NumPy module. After which, we have defined our array. Following this, we have used the print statement along with our syntax. Let us now dissect our output, compare it with our input, and try to understand it. Here for observation purposes, we will consider a flattened array, as discussed earlier.

Our input is [10,21,32,63,44,55]. Here in the subscript, we have denoted its location in the array. Now according to our command, we want to shift the elements by 2 places each. So according to that our output should be [12,23,34,65,46,57] . Here the elements 4 and 5 occupy the spots that are not part of the parent array and look. We have spots 0 and 1 empty. So these values are shifted over there, and the overall shape is retained.

Now let us look at another example and also use axis in it.

2. Example with optional parameters

import numpy as ppool


[[3 1 2]
 [5 6 4]
 [8 9 7]]

Another example of our function NumPy roll(). As similar to the first example, we have first imported the NumPy module, following which we have defined our array. Then we used our print statement with our syntax. We have also used the optional parameter axis, and what it does is rather considering the whole array it operates on its sub-arrays. Let us see what would have happened if the axis was not used.

import numpy as ppool


[[5 9 7]
 [8 1 2]
 [3 6 4]]

The difference is quiet evident. So you can play with the syntax and use it according to your need.

Must Read


In this article, we covered the NumPy roll(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy roll() is a function that this function helps us in shifting the elements within the array according to our need.

I hope this article was able to clear all of your doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read about mgrid next.

Notify of
Inline Feedbacks
View all comments