Python program to reverse a range in list
Last Updated :
14 Oct, 2024
Reversing a specific range within a list is a common task in Python programming that can be quite useful, when data needs to be rearranged. In this article, we will explore various approaches to reverse a given range within a list. We'll cover both simple and optimized methods.
One of the simplest way to reverse a range in list is by Slicing.
Example: Suppose we want to reverse a given list 'a' from index 2 to 6.
Syntax:
a[start:end] = a[start:end][::-1]
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Reverse elements from index 2 to index 6
a[2:7] = a[2:7][::-1]
print(a)
Output[1, 2, 7, 6, 5, 4, 3, 8, 9, 10]
Explaination:
- a[start:end] extracts a segment of the list a from index start to end (excluding end).
- [::-1] reverses the extracted segment.
- The reversed segment is then replace back into the original segment in the list.
Let's see other different methods to reverse a range in list.
Using Python's reversed() Function
Python’s built-in reversed() function is another way to reverse a range. However, reversed() returns an iterator, so it needs to be converted back into a list.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Reverse elements from index 2 to index 6
a[2:7] = list(reversed(a[2:7]))
print(a)
Output[1, 2, 7, 6, 5, 4, 3, 8, 9, 10]
Explanation:
- a[2:7] extracts the sublist from index 2 to 6.
- reversed(a[2:7]) creates an iterator that reverses the extracted sublist.
- list(reversed(a[2:7])) converts the reversed iterator back into a list
- Replace the original slice with the reversed list.
Note: This method is slightly longer than slicing directly but still effective.
Using a Loop
We are also reverse a range in a list by looping through the specified indices. This method provide us more control to use custom logic during reversal.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Define the start and end indices
start, end = 2, 6
# Reverse the elements from index 2 to 6
while start < end:
# Swap elements at start and end
a[start], a[end] = a[end], a[start]
# Move the start index forward
start += 1
# Move the end index backward
end -= 1
print(a)
Output[1, 2, 7, 6, 5, 4, 3, 8, 9, 10]
Explaination:
- Keep start & end pointers for the beginning and ending index of the reverse range respectively.
- Swap the elements at the start and end pointers.
- Increment start by 1 and decrement end by 1.
- Repeat step 2 and 3, until start pointer is smaller than end pointer.
Using List Comprehension
We can also use list comprehension to reverse a range, although this is less common than the other methods mentioned.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Define the start and end
start, end = 2, 6
# Use list comprehension to create the reversed segment
a[start:end+1] = [a[i] for i in range(end, start - 1, -1)]
print(a)
Output[1, 2, 7, 6, 5, 4, 3, 8, 9, 10]
Explanation:
- Use range(end, start - 1, -1) to iterate from the end index to start in reverse order.
- Construct the reversed list and assign it back to the corresponding part of the original list.
Which Method to Choose?
- Slicing Method: It is very concise, efficient, and easy to read. It works for most of the situations.
- reversed() Function: It is useful if we prefer readability and want to use the built-in functions.
- Loop Method: It provides more control for custom logic during reversal.
- List Comprehension: This method is very less readable and more complex and it is not ideal for simple reversal tasks.
Similar Reads
Python - Reverse Range in String List Given a string list, reverse each element of string list from ith to jth index. Input : test_list = ["Geeksforgeeks", "Best", "Geeks"], i, j = 1, 2 Output : ['ee', 'es', 'ee'] Explanation : Range of strings are extracted. Input : test_list = ["Geeksforgeeks"], i, j = 1, 7 Output : ['eeksfor'] Explan
3 min read
Python Program to Reverse a Number We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve
3 min read
Python Program to Reverse Every Kth row in a Matrix We are given a matrix (a list of lists) and an integer K. Our task is to reverse every Kth row in the matrix. For example:Input : a = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 4 Output : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [6, 3], [3, 7, 4], [2, 9]]Using reversed() and loopWe c
4 min read
Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes
4 min read
Python Program to Split a List into Two Halves Splitting a list into two halves is a common operation that can be useful in many cases, such as when dealing with large datasets or when performing specific algorithms (e.g., merge sort). In this article, we will discuss some of the most common methods for splitting a list into two halves.Using Lis
4 min read