
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Ways to rotate a list
List is an important container and used almost in every code of day-day programming as well as web-development, more it is used, more is the requirement to master it and hence knowledge of its operations is necessary.
Example
# using slice # initializing list test_list = [1, 4, 6, 7, 2] # printing original list print ("Original list : " + str(test_list)) # using slicing to left rotate by 3 test_list = test_list[3:] + test_list[:3] # Printing list after left rotate print ("List after left rotate by 3 : " + str(test_list)) # using slicing to right rotate by 3 # back to Original test_list = test_list[-3:] + test_list[:-3] # Printing after right rotate print ("List after right rotate by 3(back to original) : " + str(test_list)) # using list comprehension # initializing list test_list = [1, 4, 6, 7, 2] # printing original list print ("Original list : " + str(test_list)) # using list comprehension to left rotate by 3 test_list = [test_list[(i + 3) % len(test_list)] for i, x in enumerate(test_list)] # Printing list after left rotate print ("List after left rotate by 3 : " + str(test_list)) # using list comprehension to right rotate by 3 # back to Original test_list = [test_list[(i - 3) % len(test_list)] for i, x in enumerate(test_list)] # Printing after right rotate print ("List after right rotate by 3(back to original) : " + str(test_list)) # using rotate() from collections import deque # initializing list test_list = [1, 4, 6, 7, 2] # printing original list print ("Original list : " + str(test_list)) # using rotate() to left rotate by 3 test_list = deque(test_list) test_list.rotate(-3) test_list = list(test_list) # Printing list after left rotate print ("List after left rotate by 3 : " + str(test_list)) # using rotate() to right rotate by 3 # back to Original test_list = deque(test_list) test_list.rotate(3) test_list = list(test_list) # Printing after right rotate 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] 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] 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]
- Related Articles
- Python program to right rotate a list by n
- Python - Ways to flatten a 2D list
- Different ways to clear a list in Python
- Rotate a List in Java
- Python - Ways to merge strings into list
- Python - Ways to remove duplicates from list
- Python - Ways to initialize list with alphabets
- Python program to rotate doubly linked list by N nodes
- Java Program to Rotate Elements of a List
- Extending a list in Python (5 different ways)
- Python - Ways to create triplets from given list
- Python - Ways to format elements of given list
- Python - Ways to iterate tuple list of lists
- Rotate List in C++
- Python - Ways to find indices of value in list

Advertisements