Open In App

Python | Reverse All Strings in String List

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = ["geeks", "for", "geeks", "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 : ['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




# Python3 code to demonstrate
# Reverse All Strings in String List
# using map()
 
# initializing list
test_list = ["geeks", "for", "geeks", "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 : ['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




# Python3 code to demonstrate
# Reverse All Strings in String List
# using join() and reversed()
   
# initializing list
test_list = ["geeks", "for", "geeks", "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 : ['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




# define the original list of strings
test_list = ["geeks", "for", "geeks", "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 = ["geeks", "for", "geeks", "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 : ['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.



Similar Reads

Python - Find all the strings that are substrings to the given list of strings
Given two lists, the task is to write a Python program to extract all the strings which are possible substring to any of strings in another list. Example: Input : test_list1 = ["Geeksforgeeks", "best", "for", "geeks"], test_list2 = ["Geeks", "win", "or", "learn"] Output : ['Geeks', 'or'] Explanation : "Geeks" occurs in "Geeksforgeeks string as subs
5 min read
Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas provide a method to split string around a passed separator or delimiter. After that, the string can be stored as a list in a seri
3 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python | Remove empty strings from list of strings
In many scenarios, we encounter the issue of getting an empty string in a huge amount of data and handling that sometimes becomes a tedious task. Let's discuss certain way-outs to remove empty strings from list of strings. Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this
7 min read
Python | Tokenizing strings in list of strings
Sometimes, while working with data, we need to perform the string tokenization of the strings that we might get as an input as list of strings. This has a usecase in many application of Machine Learning. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + split() We can achieve this particular task using lis
3 min read
Python | Reverse Order Sort in String List
Sometimes, while working with Python, we can have a problem in which we need to perform the reverse sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + sorted() + join()
3 min read
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'] Explanation : Single string, from e to r (7 elements) ar
3 min read
Python - Sort on basis of reverse Strings
Given a String list, sort list on basis of reverse of strings. Input : test_list = ["gfg", "is", "best", "geeks"] Output : ['gfg', 'is', 'geeks', 'best'] Explanation : g < is < ks < t [elements from rear], hence the order. Input : test_list = ["gfg", "is", "best"] Output : ['gfg', 'is', 'best'] Explanation : g < s < t [elements from
5 min read
Python program to count the pairs of reverse strings
Given the String list, write a Python program to count pairs of reverse strings. Examples: Input : test_list = ["geeks", "best", "tseb", "for", "skeeg"] Output : 2 Explanation : geeks, skeeg and best, tseb are 2 pairs of reverse strings available. Input : test_list = ["geeks", "best", "for", "skeeg"] Output : 1 Explanation : geeks, skeeg is 1 pair
6 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Practice Tags :