Open In App

Python | Sort each String in String list

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Sort Strings in String list
# using list comprehension + sorted() + join()
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Sort Strings in String list
# using list comprehension + sorted() + join()
res = [''.join(sorted(ele)) for ele in test_list]
 
# printing result
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




# Python3 code to demonstrate working of
# Sort Strings in String list
# using map() + sorted() + join() + lambda
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Sort Strings in String list
# using map() + sorted() + join() + lambda
res = list(map(lambda ele: "".join(sorted(ele)), test_list))
 
# printing result
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

  1. Initialize an empty list res
  2. 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
  3. Return res.

Python3




# Python3 code to demonstrate working of
# Sort Strings in String list
# using reduce() + sorted() + join()
 
from functools import reduce
 
# initialize list
test_list = ['gfg', 'is', 'good']
 
# printing original list
print("The original list : " + str(test_list))
 
# Sort Strings in String list
# using reduce() + sorted() + join()
res = reduce(lambda x, y: x + [''.join(sorted(y))], test_list, [])
 
# printing result
print("List after string sorting : " + str(res))
#This code is contributed by Vinay Pinjala.


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads