Skip to content
Related Articles

Related Articles

Improve Article

Python | Sort each String in String list

  • Last Updated : 11 May, 2020

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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# 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']

 

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 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']



My Personal Notes arrow_drop_up
Recommended Articles
Page :