Python List sort() Method



Python List  sort()  Method
Python List  sort() 



The Sort method sorts the elements of a list in ascending or descending order.

The sort method sorts the string in alphabetical order and sorts the numeric elements from smallest to largest.

This method does not return any value.


number =  [1, 5, 3, 4, 2, 10, 6, 8, 7, 9]

number.sort()

print(' Ascending Order--> ', number)

number.sort(reverse=True)

print(' Descending Order--> ', number)


output:

Ascending Order-->[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 Descending Order-->[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]


Python List  sort()  Method
Python List  sort()  Method
                                                               

output:

Python List  sort()  Method
Python List  sort()  Method


Summary:

Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sort built-in function that builds a new sorted list from an iterable.

Sorting is done by the process of ordering or arranging a given collection of elements in some particular order.

The sort method sorts the elements in the list. This is great, except that by default sort() orders the elements in ascending—smallest values first. But what I want is the highest scores first. Luckily, you can tell the sort method to sort values in descending order largest values first. You can do this by passing True to the method’s reverse parameter









Previous
Next Post »