Sort list in Python

Sort list in Python

Python has different methods to sort elements of a list. These methods arrange elements in their natural order either in ascending or descending order. For example, if the elements are all integers, then smaller numbers go earlier in the list. If the elements are all strings, they are arranged in alphabetic order. Below are examples to sort list using list.sort(), sorted() method in ascending and descending order.

1) Sort list using list.sort()

list.sort() sorts the list in-place and returnsĀ none. So, you can’t retrieve the original positions once list.sort() has called. Below example demonstrate how to sort list using list.sort()

if __name__ == '__main__':
    #list containing numbers
    numbers = [1, 5, 3, 78, 3, 2]
    print('Unsorted List', numbers)
    numbers.sort()
    print('Sorted List : ', numbers)
    
    #list containing strings
    strings = ['Java', 'C', 'Python']
    print('Unsorted List', strings)
    strings.sort()
    print('Sorted List : ', strings)

Output

Unsorted List [1, 5, 3, 78, 3, 2]
Sorted List :  [1, 2, 3, 3, 5, 78]
Unsorted List ['Java', 'C', 'Python']
Sorted List :  ['C', 'Java', 'Python']

2) Sort list using list.sort() in reverse order

You can use list.sort() method to sort list in reverse order as well. Below is the example to sort list in reverse order using list.sort() method.

if __name__ == '__main__':
    #list containing numbers
    numbers = [1, 5, 3, 78, 3, 2]
    print('Unsorted List', numbers)
    numbers.sort(reverse=True)
    print('Sorted List : ', numbers)
    
    #list containing strings
    strings = ['Java', 'C', 'Python']
    print('Unsorted List', strings)
    strings.sort(reverse=True)
    print('Sorted List : ', strings)

Output

Unsorted List [1, 5, 3, 78, 3, 2]
Sorted List :  [78, 5, 3, 3, 2, 1]
Unsorted List ['Java', 'C', 'Python']
Sorted List :  ['Python', 'Java', 'C']

3) Sort list using sorted() method

Sorted creates a new list from the old and returns the new one. Below example demonstrate sort list using sorted() method.

if __name__ == '__main__':
    #list containing numbers
    numbers = [1, 5, 3, 78, 3, 2]
    sorted_numbers = sorted(numbers)
    print('Unsorted List', numbers)
    print('Sorted List : ', sorted_numbers)
    
    #list containing strings
    strings = ['Java', 'C', 'Python']
    sorted_strings = sorted(strings)
    print('Unsorted List', strings)
    print('Sorted List : ', sorted_strings)

Output

Unsorted List [1, 5, 3, 78, 3, 2]
Sorted List :  [1, 2, 3, 3, 5, 78]
Unsorted List ['Java', 'C', 'Python']
Sorted List :  ['C', 'Java', 'Python']

4) Sort list using sorted() method in reverse order

You can sort list in reverse order using sorted method by passing parameter reverse. By default the value of reverse is false. Below example demonstrate sort list using sorted() method in reverse order.

if __name__ == '__main__':
    #list containing numbers
    numbers = [1, 5, 3, 78, 3, 2]
    sorted_numbers = sorted(numbers, reverse=True)
    print('Unsorted List', numbers)
    print('Sorted List : ', sorted_numbers)
    
    #list containing strings
    strings = ['Java', 'C', 'Python']
    sorted_strings = sorted(strings, reverse=True)
    print('Unsorted List', strings)
    print('Sorted List : ', sorted_strings)

Output

Unsorted List [1, 5, 3, 78, 3, 2]
Sorted List :  [78, 5, 3, 3, 2, 1]
Unsorted List ['Java', 'C', 'Python']
Sorted List :  ['Python', 'Java', 'C']

Stay tuned for more updates and tutorials !!!

Leave a Reply

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