Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Aug, 2022
View Discussion
Improve Article
Save Article

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

Python3




# Python code to demonstrate
# to sort list of
# strings in case insensitive manner
 
# Initialising list
ini_list = ['akshat', 'garg', 'GeeksForGeeks', 'Alind',
            'SIngh', 'manjeet', 'Munich']
 
# Sorting list in case sensitive manner
res1 = sorted(ini_list)
  
# Printing case-sensitive
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() 
 

Python3




# Python code to demonstrate
# to sort list of
# strings in case insensitive manner
 
# Initialising list
ini_list = ['akshat', 'garg', 'GeeksForGeeks', 'Alind',
            'SIngh', 'manjeet', 'Munich']
 
# Sorting list in case sensitive manner
ini_list.sort()
  
# Printing case-sensitive
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’] 
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!