Method Name:
rotate
Method Signature:
rotate[n]
Method Overview:
- rotate[] method of python deque class rotates the sequence present in the deque object ‘n’ times.
- If no parameter is passed ‘n’ will have the default value of 1.
- If ‘n’ is negative rotation direction is right to left.
- If ‘n’ is positive rotation direction is left to right.
- The rotate[] method does not return a copy of the deque which has the new sequence. Instead, rotation is applied on the contents of the deque object itself.
Example 1 – Rotate the Python Deque Object positive number of times:
import collections
# Create an empty deque object
dequeObject = collections.deque[]
# Add elements to the deque - left to right
dequeObject.append[1]
dequeObject.append[3]
dequeObject.append[6]
dequeObject.append[10]
# Print the deque contents
print["Deque before any rotation:"]
print[dequeObject]
# Rotate once in positive direction
dequeObject.rotate[]
print["Deque after 1 positive rotation:"]
print[dequeObject]
# Rotate twice in positive direction
dequeObject.rotate[2]
print["Deque after 2 positive rotations:"]
print[dequeObject]
Output:
Deque before any rotation:
deque[[1, 3, 6, 10]]
Deque after 1 positive rotation:
deque[[10, 1, 3, 6]]
Deque after 2 positive rotations:
deque[[3, 6, 10, 1]]
The output shows that effectively 3 positive rotations applied on the sequence of elements present in the deque object.
Example 2 – Rotate the Python Deque Object negative number of times:
import collections
# Create a tuple
sequence = [1, 2, 4, 8, 16, 32]
# Create a deque from the tuple
sequenceInDeque = collections.deque[sequence]
# Print the contents of the deque object
print["Deque before any rotation:"]
print[sequenceInDeque]
# Rotate once - in negative direction
sequenceInDeque.rotate[-1]
print["Deque after 1 negative rotation:"]
print[sequenceInDeque]
# Rotate twice - in negative direction
sequenceInDeque.rotate[-2]
print["Deque after 2 negative rotations:"]
print[sequenceInDeque]
Output:
Deque before any rotation:
deque[[1, 2, 4, 8, 16, 32]]
Deque after 1 negative rotation:
deque[[2, 4, 8, 16, 32, 1]]
Deque after 2 negative rotations:
deque[[8, 16, 32, 1, 2, 4]]
The output shows that effectively 3 negative rotations applied on the sequence of elements present in the deque object.
The rotation of a list has been discussed earlier also, but this particular article focuses on shorthands and various short techniques to achieve this in one-liners or one word. This operation is quite essential in a programmer’s life to achieve various tasks. Let’s discuss different ways we can rotate a list in Python.
Method 1: Rotate a list using Slicing
This particular method is the generic method and is mostly employed to achieve this task and has also been discussed in many articles as well. It works by just joining the later sliced part to the initial sliced part given the rotation number.
Python3
test_list
=
[
1
,
4
,
6
,
7
,
2
]
print
[
"Original list : "
+
str
[test_list]]
test_list
=
test_list[
3
:]
+
test_list[:
3
]
print
[
"List after left rotate by 3 : "
+
str
[test_list]]
test_list
=
test_list[
-
3
:]
+
test_list[:
-
3
]
print
[
"List after right rotate by 3[back to original] : "
+
str
[test_list]]
Output:
Original list : [1, 4, 6, 7, 2] List after left rotate by 3 : [7, 2, 1, 4, 6] List after right rotate by 3 [ back to original] : [1, 4, 6, 7, 2]
Method 2: Rotate a list using list Comprehension
This problem can also be solved by the naive method, but its shorter implementation would be with the help of list comprehension. In this method, we just reassign the index to each value to the specific position after rotation.
Python3
test_list
=
[
1
,
4
,
6
,
7
,
2
]
print
[
"Original list : "
+
str
[test_list]]
test_list
=
[test_list[[i
+
3
]
%
len
[test_list]]
for
i, x
in
enumerate
[test_list]]
print
[
"List after left rotate by 3 : "
+
str
[test_list]]
test_list
=
[test_list[[i
-
3
]
%
len
[test_list]]
for
i, x
in
enumerate
[test_list]]
print
[
"List after right rotate by 3[back to original] : "
+
str
[test_list]]
Output:
Original list : [1, 4, 6, 7, 2] List after left rotate by 3 : [7, 2, 1, 4, 6] List after right rotate by 3[back to original] : [1, 4, 6, 7, 2]
Method 3: Rotate a list using collections.deque.rotate[]
The collections module has a deque class that provides the rotate[], which is an inbuilt function to allow rotation. This is a lesser-known function but has a greater utility.
Python3
from
collections
import
deque
test_list
=
[
1
,
4
,
6
,
7
,
2
]
print
[
"Original list : "
+
str
[test_list]]
test_list
=
deque[test_list]
test_list.rotate[
-
3
]
test_list
=
list
[test_list]
print
[
"List after left rotate by 3 : "
+
str
[test_list]]
test_list
=
deque[test_list]
test_list.rotate[
3
]
test_list
=
list
[test_list]
print
[
"List after right rotate by 3[back to original] : "
+
str
[test_list]]
Output:
Original list : [1, 4, 6, 7, 2] List after left rotate by 3 : [7, 2, 1, 4, 6] List after right rotate by 3[back to original] : [1, 4, 6, 7, 2]
Method 4: Rotate a list using Numpy
In this method, we will use Numpy.roll module to roll the list at a given position, i.e. we are rolling the list at position index 1.
Python3
import
numpy as np
if
__name__
=
=
'__main__'
:
nums
=
[
11
,
4
,
6
,
7
,
8
,
33
]
k
=
1
x
=
np.roll[nums, k]
print
[x]
Output:
[33 11 4 6 7 8]