Skip to content
Related Articles

Related Articles

Python | Ways to sort list of strings in case-insensitive manner
  • Last Updated : 11 May, 2020

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()




# 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))
         
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()




# 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))
     
Output:



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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :