Python | Reverse All Strings in String List
Last Updated :
16 May, 2023
Given a list, we always come across situations in which we require to apply certain function to each element in a list. This can be easily done by applying a loop and performing an operation to each element. But having shorthands to solve this problem is always beneficial and helps to focus more on important aspects of problem. Let’s discuss certain ways in which reverse operation on each string in string list can be performed.
Method #1 : Using list comprehension This method performs the same task in the background as done by the looping constructs. The advantage this particular method provides is that this is a one liner and also improves code readability.
Python3
test_list = [ "geeks" , "for" , "geeks" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
res = [i[:: - 1 ] for i in test_list]
print ( "The reversed string list is : " + str (res))
|
Output :
The original list is : ['geeks', 'for', 'geeks', 'is', 'best']
The reversed string list is : ['skeeg', 'rof', 'skeeg', 'si', 'tseb']
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using map() Using map function, one can easily associate an element with the operation one wishes to perform. This is the most elegant way to perform or solve this kind of problems.
Python3
test_list = ["geeks", " for ", "geeks", " is ", "best"]
print ("The original list is : " + str (test_list))
reverse = lambda i : i[:: - 1 ]
res = list ( map (reverse, test_list))
print ("The reversed string list is : " + str (res))
|
Output :
The original list is : ['geeks', 'for', 'geeks', 'is', 'best']
The reversed string list is : ['skeeg', 'rof', 'skeeg', 'si', 'tseb']
Time complexity: O(n), where n is the length of the input list. This is because the map function is applied to each element of the list exactly once, which takes constant time.
Auxiliary space: O(n), where n is the length of the input list. This is because the program creates a new list to store the resulting reversed strings, which has the same length as the input list.
Method #3 : Using join() and reversed()
This method uses the join and reversed function to perform the reverse on each string in the list.
Python3
test_list = [ "geeks" , "for" , "geeks" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
res = [''.join( reversed (i)) for i in test_list]
print ( "The reversed string list is : " + str (res))
|
Output
The original list is : ['geeks', 'for', 'geeks', 'is', 'best']
The reversed string list is : ['skeeg', 'rof', 'skeeg', 'si', 'tseb']
Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the list.
Method 4: Using a for loop and slicing:
Creates an empty list res to store the reversed strings, then iterates over each string in the original list test_list. For each string, it uses slicing to reverse the characters in the string and appends the result to the res list. Finally, it prints the list of reversed strings.
Python3
test_list = [ "geeks" , "for" , "geeks" , "is" , "best" ]
res = []
for string in test_list:
res.append(string[:: - 1 ])
print (res)
|
Output
['skeeg', 'rof', 'skeeg', 'si', 'tseb']
Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), where n is the length of the list.
Method 5: Using reduce
In this method we starts by defining a function named reverse_string() which reverses a string. Then, it initializes a list of strings. The reduce() function is used to apply the reverse_string() function on each string of the list. The reduce() function iteratively applies the given function on each element in the list and aggregates the result. The result is a list of reversed strings which is then printed.
Python3
from functools import reduce
def reverse_string(s):
return s[:: - 1 ]
test_list = [ "geeks" , "for" , "geeks" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x + [reverse_string(y)], test_list, [])
print ( "The reversed string list is : " + str (res))
|
Output
The original list is : ['geeks', 'for', 'geeks', 'is', 'best']
The reversed string list is : ['skeeg', 'rof', 'skeeg', 'si', 'tseb']
Time Complexity: O(n), as reduce() iterates through each element in the input list once.
Auxiliary Space: O(n), where n is the length of the input list.
Please Login to comment...