Given a List, our task is to write a Python program to reverse a range in the list.
Reverse a range in a list Example
Input : test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11], str, end = 3, 9
Output : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11]
Explanation : 8, 9, 2, 10, 12, 7 are reversed in list to 7, 12, 10, 2, 9, 8.
Input : test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11], str, end = 8, 9
Output : [6, 3, 1, 8, 9, 2, 10, 7, 12, 4, 11]
Explanation : 12, 7 are reversed in list to 7, 12.
Reverse a range in a list using reverse()
In this example, the sublist is extracted and reversed using reverse(). The loop is used next to replace range elements with reversed elements using Python.
Python3
test_list = [ 6 , 3 , 1 , 8 , 9 , 2 , 10 , 12 , 7 , 4 , 11 ]
print ( "The original list is : " + str (test_list))
strt, end = 3 , 9
temp = test_list[strt:end]
temp.reverse()
for idx in range (strt, end):
test_list[idx] = temp[idx - strt]
print ( "Range reversed range list : " + str (test_list))
|
Output:
The original list is : [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
Range reversed range list : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11]
Time Complexity: O(n)
Auxiliary Space: O(n)
Reverse a range in a list using list split() + slicing
The compact approach to solve this problem is to perform a reversal of the range list using a Python split() way of reversing slicing only the required range.
Python3
test_list = [ 6 , 3 , 1 , 8 , 9 , 2 , 10 , 12 , 7 , 4 , 11 ]
print ( "The original list is : " + str (test_list))
strt, end = 3 , 9
test_list[strt:end] = test_list[strt:end][:: - 1 ]
print ( "Range reversed range list : " + str (test_list))
|
Output:
The original list is : [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
Range reversed range list : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11]
Time Complexity: O(n)
Auxiliary Space: O(1)
Reverse a range in a list using slicing+extend()+reverse()
Here, the third argument of a split with -1 performs the reverse of the Python list.
Python3
test_list = [ 6 , 3 , 1 , 8 , 9 , 2 , 10 , 12 , 7 , 4 , 11 ]
print ( "The original list is : " + str (test_list))
strt, end = 3 , 9
a = test_list[ 0 :strt]
x = test_list[strt:end]
b = test_list[end: len (test_list)]
x.reverse()
a.extend(x)
a.extend(b)
print ( "Range reversed range list : " + str (a))
|
Output:
The original list is : [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
Range reversed range list : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11]
Time Complexity: O(n)
Auxiliary Space: O(n)
Reverse a range in a list using Numpy
In this approach, we will use the Numpy library to perform the desired operation.
Note: Install the Numpy module using the command “pip install numpy”
Python3
import numpy as np
test_list = [ 6 , 3 , 1 , 8 , 9 , 2 , 10 , 12 , 7 , 4 , 11 ]
print ( "The original list is : " + str (test_list))
strt, end = 3 , 9
res = test_list[ 0 :strt] + list (
np.flip(test_list[strt:end])) + test_list[end: len (
test_list)]
print ( "Range reversed range list : " + str (res))
|
Output:
The original list is : [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
Range reversed range list : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11
Time Complexity: O(n)
Auxiliary Space: O(n)
Reverse a range in a list using reduce() method:
Algorithm :
- Initialize the list “test_list”.
- Print the original list “test_list”.
- Initialize the range of indices to be reversed in the original list as “strt” and “end”.
- Reverse the sublist of “test_list” from “strt” to “end” using the NumPy flip() method.
- Convert the reversed sublist to a list using the list() method.
- Slice the “test_list” from the start to “strt” and concatenate it with the reversed sublist and the sliced list from “end” to the end of the list.
- Assign the result to the variable “res”.
- Print the result “res”.
Python3
from functools import reduce
test_list = [ 6 , 3 , 1 , 8 , 9 , 2 , 10 , 12 , 7 , 4 , 11 ]
print ( "The original list is : " + str (test_list))
strt, end = 3 , 9
res = reduce ( lambda x, y: x + [y], test_list[:strt], []) + \
list ( reversed (test_list[strt:end])) + \
reduce ( lambda x, y: x + [y], test_list[end:], [])
print ( "Range reversed range list : " + str (res))
|
Output
The original list is : [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
Range reversed range list : [6, 3, 1, 7, 12, 10, 2, 9, 8, 4, 11]
Time complexity: O(n), where n is the length of the input list. The reverse operation takes O(m), where m is the size of the slice being reversed, but since m is at most n, it does not affect the overall time complexity.
Space complexity: O(n), since the output list has the same length as the input list.
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...