Python | Sort each String in String list
Last Updated :
09 Apr, 2023
Sometimes, while working with Python, we can have a problem in which we need to perform the 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() This is one way in which this problem can be solved. In this, we use sorted() functionality to perform sort operation and join() is used to reconstruct the string list.
Python3
test_list = [ 'gfg' , 'is' , 'good' ]
print ("The original list : " + str (test_list))
res = [''.join( sorted (ele)) for ele in test_list]
print (" List after string sorting : " + str (res))
|
Output :
The original list : ['gfg', 'is', 'good']
List after string sorting : ['fgg', 'is', 'dgoo']
Time Complexity: O(n*nlogn) where n is the number of elements in the string list. The list comprehension + sorted() + join() is used to perform the task and it takes O(n*nlogn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using map() + sorted() + join() + lambda The combination of above method can also be used to perform this task. In this, we perform the functionality of traversal using map() and lambda rather than list comprehension.
Python3
test_list = [ 'gfg' , 'is' , 'good' ]
print ("The original list : " + str (test_list))
res = list ( map ( lambda ele: "".join( sorted (ele)), test_list))
print (" List after string sorting : " + str (res))
|
Output :
The original list : ['gfg', 'is', 'good']
List after string sorting : ['fgg', 'is', 'dgoo']
Time Complexity: O(n*nlogn), where n is the length of the input list. This is because we’re using map() + sorted() + join() + lambda which has a time complexity of O(n*nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3: Using reduce()
Algorithm
- Initialize an empty list res
- For each string s in test_list, do the following:
a. Sort the characters in s using sorted()
b. Join the sorted characters back together into a string using join()
c. Append the sorted string to res
- Return res.
Python3
from functools import reduce
test_list = [ 'gfg' , 'is' , 'good' ]
print ( "The original list : " + str (test_list))
res = reduce ( lambda x, y: x + [''.join( sorted (y))], test_list, [])
print ( "List after string sorting : " + str (res))
|
Output
The original list : ['gfg', 'is', 'good']
List after string sorting : ['fgg', 'is', 'dgoo']
Time complexity: O(n * m * log(m)), where n is the length of the list test_list and m is the maximum length of a string in test_list. This is because sorted() takes O(m * log(m)) time to sort each string in test_list, and reduce() takes O(n) time to iterate over each element in test_list.
Space complexity: O(n * m), where n is the length of the list test_list and m is the maximum length of a string in test_list. This is because the reduce() function creates a new list res with one element for each element in test_list, and each element in res takes up to m characters.
Please Login to comment...