Open In App

Python program to Reverse a range in list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# initializing list
test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
strt, end = 3, 9
 
# reversing list and assigning the range
temp = test_list[strt:end]
temp.reverse()
for idx in range(strt, end):
    test_list[idx] = temp[idx - strt]
         
# printing result
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




# Python3 code to demonstrate working of
# Reversing a range
# Using list split + slicing
 
# initializing list
test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
              
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
strt, end = 3, 9
 
# Third arg. of split with -1 performs reverse
test_list[strt:end] = test_list[strt:end][::-1]
 
# printing result
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




# initializing list
test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
             
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
strt, end = 3, 9
 
# Third arg. of split with -1 performs reverse
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)
 
# printing result
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




# importing numpy
import numpy as np
 
# initializing list
test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
strt, end = 3, 9
 
# using list slicing
res = test_list[0:strt] +list(
      np.flip(test_list[strt:end])) + test_list[end:len(
                                          test_list)]
 
# printing result
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 :

  1. Initialize the list “test_list”.
  2. Print the original list “test_list”.
  3. Initialize the range of indices to be reversed in the original list as “strt” and “end”.
  4. Reverse the sublist of “test_list” from “strt” to “end” using the NumPy flip() method.
  5. Convert the reversed sublist to a list using the list() method.
  6. 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.
  7. Assign the result to the variable “res”.
  8. Print the result “res”.

Python3




from functools import reduce
 
# initializing list
test_list = [6, 3, 1, 8, 9, 2, 10, 12, 7, 4, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
strt, end = 3, 9
 
# using reduce and slicing
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:], [])
 
# printing result
print("Range reversed range list : " + str(res))
#This code is contributed by Jyothi pinjala.


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
Previous
Next
Share your thoughts in the comments
Similar Reads