Python - Reverse Range in String List
Last Updated :
23 Apr, 2023
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'] Explanation : Single string, from e to r (7 elements) are sliced.
Method #1 : Using loop + reversed() + string slicing
The combination of above methods can be used to solve this problem. In this, we perform reverse of range using reversed() extracted using slicing.
Python3
# Python3 code to demonstrate working of
# Common list elements and dictionary values
# Using set() and intersection()
# initializing list
test_list = ["Geeksforgeeks", "Best", "Geeks"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 1, 3
res = []
for ele in test_list:
# slicing and appending range
res.append(ele[i : j + 1])
# printing result
print("Sliced strings : " + str(res))
OutputThe original list : ['Geeksforgeeks', 'Best', 'Geeks']
Sliced strings : ['eek', 'est', 'eek']
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using map() + slicing + lambda
This is yet another way in which this task can be performed. In this, we use map() to extend the logic of slice made using lambda to entire list.
Python3
# Python3 code to demonstrate working of
# Common list elements and dictionary values
# Using map() + slicing + lambda
# initializing list
test_list = ["Geeksforgeeks", "Best", "Geeks"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 1, 3
# map used to extend logic to each string
res = list(map(lambda x : x[i : j + 1], test_list))
# printing result
print("Sliced strings : " + str(res))
OutputThe original list : ['Geeksforgeeks', 'Best', 'Geeks']
Sliced strings : ['eek', 'est', 'eek']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 3 : using list comprehension,
Initialize the list of strings test_list.
Initialize the values of i and j to slice each string in the list.
Use a list comprehension to extract the slice from each string in test_list.
Store the extracted slices in a new list res.
Print the original list and the sliced strings.
Python3
# Python3 code to demonstrate working of
# Common list elements and dictionary values
# Using list comprehension and string slicing
# initializing list
test_list = ["Geeksforgeeks", "Best", "Geeks"]
# printing original list
print("The original list : " + str(test_list))
# initializing range
i, j = 1, 3
# using list comprehension to slice each string
res = [s[i:j+1] for s in test_list]
# printing result
print("Sliced strings : " + str(res))
OutputThe original list : ['Geeksforgeeks', 'Best', 'Geeks']
Sliced strings : ['eek', 'est', 'eek']
The time complexity of this approach is O(n), where n is the length of the list test_list, because we are iterating through each string in the list once.
The auxiliary space complexity is also O(n), because we are storing the extracted slices in a new list.
Similar Reads
Python program to reverse a range in list
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 w
3 min read
Python | Convert String ranges to list
Sometimes, while working in applications we can have a problem in which we are given a naive string that provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let's discuss certain ways in which this problem can be solved. Method #1:
6 min read
Reverse All Strings in String List in Python
We are given a list of strings and our task is to reverse each string in the list while keeping the order of the list itself unchanged. For example, if we have a list like this: ['gfg', 'is', 'best'] then the output will be ['gfg', 'si', 'tseb'].Using For LoopWe can use a for loop to iterate over th
2 min read
How To Reverse A List In Python Using Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. In this article, we will see how we can reverse a list using slicing in Python. Reverse a List Using Slicing in PythonBelow are some of the examples by which we can perform rev
3 min read
Reverse Sort a String - Python
The goal is to take a given string and arrange its characters in descending order based on their Unicode values. For example, in the string "geeksforgeeks", the characters will be sorted from highest to lowest, resulting in a new string like "ssrokkggfeeeee". Let's understand different methods to pe
2 min read
List of strings in Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
Python - Extract range sized strings
Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific range sized strings. This kind of problem can occur during validation cases across many domains. Letâs discuss certain ways to handle this in Python strings list. Method #1 : Using list
4 min read
Python - Reverse Slicing of given string
Reverse slicing in Python is a way to access string elements in reverse order using negative steps.Using Slicing ([::-1])Using slicing with [::-1] in Python, we can reverse a string or list. This technique works by specifying a step of -1, which starts at the end of the sequence and moves backward,
1 min read
Python | Reverse Interval Slicing String
Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this t
4 min read
Python - Remove leading 0 from Strings List
Sometimes, while working with Python, we can have a problem in which we have data which we need to perform processing and then pass the data forward. One way to process is to remove a stray 0 that may get attached to a string while data transfer. Let's discuss certain ways in which this task can be
5 min read