In Python, the easiest way to rotate items in a list is with the Python list pop(), insert(), and append() functions.

lst = [0,1,2,3]

#rotated backwards (to left)
lst.append(lst.pop(0))
print(lst)

lst= [0,1,2,3]

#rotated forward (to right)
lst.insert(0,lst.pop())
print(lst)

#Output:
[1,2,3,0]
[3,0,1,2]

You can also use the deque() data structure from the Python collections module to rotate a list.

from collections import deque

items = deque([0,1,2,3])

#rotated backwards (to left)
items.rotate(-1)

print(items)

items = deque([0,1,2,3])

#rotated forward (to right)
items.rotate(1)

print(items)

#Output:
deque([1,2,3,0])
deque([3,0,1,2])

You can also use list slicing to rotate a list forward or backwards in Python.

lst= [0,1,2,3]

list_rotated_backwards = lst[1:] + lst[:1]
list_rotated_forward = lst[-1:] + lst[:-1]

print(list_rotated_backwards)
print(list_rotated_forward)

#Output:
[1,2,3,0]
[3,0,1,2]

In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to change the order of items of a list in an easy way.

With Python, we can easily rotate the items in a list both to the right or the left.

To rotate items to the left, we can remove the first element from the list with pop(), and then append it to the end of the list with the append() function.

To rotate items to the right, we can do the opposite. Rotating to the right involves removing the last element from the list, and then prepending it to the beginning of the list.

Below is an example in Python of how to rotate values in a list using the pop(), append(), and insert() functions.

lst= [0,1,2,3]

#rotated backwards (to left)
lst.append(lst.pop(0))
print(lst)

lst= [0,1,2,3]

#rotated forward (to right)
lst.insert(0,lst.pop())
print(lst)

#Output:
[1,2,3,0]
[3,0,1,2]

If you need to rotate a list multiple times, you can use a loop and apply these operations as many times as needed.

Below is a function which will rotate values in a list multiple times to the left or right depending on the argument values passed.

def rotateList(lst,direction,n):
    if direction == "backwards":
        for i in range(0,n):
            lst.append(lst.pop(0))
    else: 
        for i in range(0,n):
            lst.insert(0,lst.pop())
    return lst

print(rotateList([0,1,2,3,4,5,6],"backwards",2))
print(rotateList([0,1,2,3,4,5,6],"forwards",3))

#Output:
[2, 3, 4, 5, 6, 0, 1]
[4, 5, 6, 0, 1, 2, 3]

Using deque in Python to Rotate a List

Another way that you can rotate a list is with the deque data structure from the Python collections module.

Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster and more efficient than the similar list operations.

To rotate the elements of a list, we can convert it to a deque object and then use the rotate() function.

Below are some examples of how to rotate items in a list with the deque rotate() function.

from collections import deque

items = deque([0,1,2,3])

#rotated backwards (to left)
items.rotate(-1)

print(items)

items = deque([0,1,2,3])

#rotated forward (to right)
items.rotate(1)

print(items)

#Output:
deque([1,2,3,0])
deque([3,0,1,2])

If you want to rotate the items multiple times, you just pass the number of times to rotate().

from collections import deque

items = deque([0,1,2,3,4,5,6])

#rotateed backwards (to left)
items.rotate(-3)

print(items)

items = deque([0,1,2,3,4,5,6])

#rotateed forward (to right)
items.rotate(2)

print(items)

#Output:
deque([3, 4, 5, 6, 0, 1, 2])
deque([5, 6, 0, 1, 2, 3, 4])

Rotating a List in Python with Slicing

You can also rotate the items in a list in Python using list slicing.

To rotate a list backwards, we slice the list from the second element to the end, and then add a slice with only the first element to the end of first slice.

To rotate a list forward, we slice the list from the second to last element to the beginning, and then add a slice with only the last element to the beginning of first slice.

Below is an example of how to rotate a list both backwards and forward with list slicing using Python.

lst= [0,1,2,3]

list_rotated_backwards = lst[1:] + lst[:1]
list_rotated_forward = lst[-1:] + lst[:-1]

print(list_rotated_backwards)
print(list_rotated_forward)

#Output:
[1,2,3,0]
[3,0,1,2]

If you need to rotate a list multiple times, we can define a function which rotates the list a specified number of items.

Below is a function which will rotate the items in a list using slicing multiple times to the left or right depending on the argument values passed.

def rotateList(lst,direction,n):
    if direction == "backwards":
        new_list = lst[n:] + lst[:n]
    else: 
        new_list = lst[-n:] + lst[:-n]
    return new_list

print(rotateList([0,1,2,3,4,5,6],"backwards",2))
print(rotateList([0,1,2,3,4,5,6],"forwards",3))

#Output:
[2, 3, 4, 5, 6, 0, 1]
[4, 5, 6, 0, 1, 2, 3]

Hopefully this article has been useful for you to learn how to rotate lists in Python.

Categorized in:

Python,

Last Update: April 2, 2024