Python : How to Sort a list of strings ? | list.sort() Tutorial & Examples

In this article we will discuss how to sort a list of string by,

  • By Alphabetical Order
  • By Reverse Alphabetical Order
  • By String Length
  • By Numeric Order

list.sort()

list provides a member function sort(). It Sorts the elements of list in low to high order i.e. if list is of numbers then by default they will be sorted in increasing order. Whereas, if list is of strings then, it will sort them in alphabetical order.

Suppose we have a list of strings i.e.

#List Of Strings
listOfStrings = ['hi' , 'hello', 'at', 'this', 'there', 'from']

Let’s sort this list of strings in different way i.e.

Sort a List of strings in Alphabetical Order

'''
Sort List of string alphabetically
'''
listOfStrings.sort()

It will sort the list in alphabetically i.e.

['at', 'from', 'hello', 'hi', 'there', 'this']

Sort a List of strings alphabetically in Reverse Order

list.sort(reverse=True)

list.sort() accepts an another argument reverse. By default its value is False, but if its set to True then it will sort the list in reverse order.

So, contents of list will be now,

['this', 'there', 'hi', 'hello', 'from', 'at']

Sort a List of string by Length

list.sort( key=function )

list.sort() accepts an another argument key i.e. key Function. While sorting a list, all the elements of list will be compared with each other. Before comparison it will call the key function on each entry,  to determine what should be compared.

For example,

To Sort a list of strings by length, provide len() as key function in sort i.e.

'''
Sort List of string by Length by using len() as custom key function 
'''
listOfStrings.sort(key=len)

Now list contents will be,

['hi', 'at', 'this', 'from', 'there', 'hello']

Sort a List of string by Numeric Order

Suppose w e have a list of strings that contains numbers i.e.

listOfNum = ['55' , '101', '152', '98', '233', '40', '67']

To Sort a this list of strings by Numeric Order, provide int() as key function in sort i.e.

'''
Sort in Ascending numeric order, pass key function that should convert string to integer i.e using int()
'''
listOfNum.sort(key=int)

Now list contents will be,

['40', '55', '67', '98', '101', '152', '233']

Sorting a list of strings by Numerically in descending Order

To Sort in Descending numeric order, pass reverse flag along with key function i.e.

'''
Sort in Descending numeric order, pass reverse flag along with key function
'''
listOfNum.sort(reverse=True, key=int)

Now list contents will be,

['233', '152', '101', '98', '67', '55', '40']

Complete example is as follows,

def main():
    
    #List Of Strings
    listOfStrings = ['hi' , 'hello', 'at', 'this', 'there', 'from']
    
    print(listOfStrings)
    
    '''
    Sort List of string alphabetically
    '''
    listOfStrings.sort()
    
    # Print the list
    print(listOfStrings)
    
    '''
    Sort List of string alphabetically in Reverse Order
    '''
    listOfStrings.sort(reverse=True)
    
    print(listOfStrings)
    
    '''
    Sort List of string by Length by using len() as custom key function 
    '''
    listOfStrings.sort(key=len)
    
    print(listOfStrings)
    
    
    '''
    Sort List of string by Numeric Order
    '''
    listOfNum = ['55' , '101', '152', '98', '233', '40', '67']
    
    # It will sort in alphabetical order
    listOfNum.sort()
    
    print(listOfNum)
    
    '''
    Sort in Ascending numeric order, pass key function that should convert string to integer i.e using int()
    '''
    listOfNum.sort(key=int)
    
    print(listOfNum)
    
    '''
    Sort in Descending numeric order, pass reverse flag along with key function
    '''
    
    listOfNum.sort(reverse=True, key=int)
    
    
    print(listOfNum)
    
    
if __name__ == '__main__':
    main()

Output:

['hi', 'hello', 'at', 'this', 'there', 'from']
['at', 'from', 'hello', 'hi', 'there', 'this']
['this', 'there', 'hi', 'hello', 'from', 'at']
['hi', 'at', 'this', 'from', 'there', 'hello']
['101', '152', '233', '40', '55', '67', '98']
['40', '55', '67', '98', '101', '152', '233']
['233', '152', '101', '98', '67', '55', '40']

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top