Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Reverse All Strings in String List

Python | Reverse All Strings in String List

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




# Python3 code to demonstrate
# Reverse All Strings in String List
# using list comprehension
 
# initializing list
test_list = ["Lazyroar", "for", "Lazyroar", "is", "best"]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list comprehension
# Reverse All Strings in String List
res = [i[::-1] for i in test_list]
 
# printing result
print ("The reversed string list is : " + str(res))


Output : 

The original list is : ['Lazyroar', 'for', 'Lazyroar', '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




# Python3 code to demonstrate
# Reverse All Strings in String List
# using map()
 
# initializing list
test_list = ["Lazyroar", "for", "Lazyroar", "is", "best"]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# Reverse All Strings in String List
# using map()
reverse = lambda i : i[::-1]
res = list(map(reverse, test_list))
 
# printing result
print ("The reversed string list is : " + str(res))


Output : 

The original list is : ['Lazyroar', 'for', 'Lazyroar', '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




# Python3 code to demonstrate
# Reverse All Strings in String List
# using join() and reversed()
   
# initializing list
test_list = ["Lazyroar", "for", "Lazyroar", "is", "best"]
   
# printing original list
print ("The original list is : " + str(test_list))
   
# using join() and reversed()
res = [''.join(reversed(i)) for i in test_list]
   
# printing result
print ("The reversed string list is : " + str(res))


Output

The original list is : ['Lazyroar', 'for', 'Lazyroar', '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




# define the original list of strings
test_list = ["Lazyroar", "for", "Lazyroar", "is", "best"]
 
# create an empty list to store the reversed strings
res = []
 
# iterate over each string in the original list
for string in test_list:
    # use slicing to reverse the string and append it to the result list
    res.append(string[::-1])
 
# print the list of reversed strings
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
 
# function to reverse the string
def reverse_string(s):
    return s[::-1]
 
# initializing list
test_list = ["Lazyroar", "for", "Lazyroar", "is", "best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce() to apply the reverse_string function on each string in the list
res = reduce(lambda x, y: x + [reverse_string(y)], test_list, [])
 
# printing result
print("The reversed string list is : " + str(res))


Output

The original list is : ['Lazyroar', 'for', 'Lazyroar', '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.

RELATED ARTICLES

Most Popular

Recent Comments