Python | Ways to sort list of strings in case-insensitive manner

Given a list of strings, A task is to sort the strings in a case insensitive manner. Given below are a few methods to solve the task.
Method #1: Using casefold()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# to sort list of
# strings in case insensitive manner
  
# Initialsing list
ini_list = ['akshat', 'garg', 'GeeksForGeeks', 'Alind',
            'SIngh', 'manjeet', 'Munich']
  
# Sorting list in case sensitive manner
res1 = sorted(ini_list)
   
# Printing case-insensitive
print("Case-sensitive sorted list", str(res1))
  
# Sorting list in case-insensitive manner
res2 = sorted(ini_list, key = lambda s: s.casefold())
  
# Printing result
print("Case-insensitive sorted list", str(res2))
         

chevron_right


Output:

Case-sensitive sorted list [‘Alind’, ‘GeeksForGeeks’, ‘Munich’, ‘SIngh’, ‘akshat’, ‘garg’, ‘manjeet’]
Case-insensitive sorted list [‘akshat’, ‘Alind’, ‘garg’, ‘GeeksForGeeks’, ‘manjeet’, ‘Munich’, ‘SIngh’]



 
Method #2: Using lower()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate 
# to sort list of
# strings in case insensitive manner
  
# Initialsing list
ini_list = ['akshat', 'garg', 'GeeksForGeeks', 'Alind',
            'SIngh', 'manjeet', 'Munich']
  
# Sorting list in case sensitive manner
ini_list.sort()
   
# Printing case-insensitive
print("Case-sensitive sorted list", str(ini_list))
  
# Sorting list in case-insensitive manner
ini_list.sort(key = lambda x: x.lower())
  
# Printing result
print("Case-insensitive sorted list", str(ini_list))
     

chevron_right


Output:

Case-sensitive sorted list [‘Alind’, ‘GeeksForGeeks’, ‘Munich’, ‘SIngh’, ‘akshat’, ‘garg’, ‘manjeet’]
Case-insensitive sorted list [‘akshat’, ‘Alind’, ‘garg’, ‘GeeksForGeeks’, ‘manjeet’, ‘Munich’, ‘SIngh’]



My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.