Python | sort list

python sort list,python sort list function
python sort list

Sort list

sort() Method is sort the list alphanumerically , ascending order to sort by default.


Example

    list = [ 'mango' , 'apple', 'kiwi' , 'orange' , 'pineapple']

    list.sort() 

    print(list)        


Output

    ['apple', 'kiwi', 'mango', 'orange', 'pineapple']



If sort to descending order, set argument set in sort method ' reverse = True '

Example

    list = [ 'mango' , 'apple', 'kiwi' , 'orange' , 'pineapple']

    list.sort( reverse = True ) 

    print(list)       


Output

    ['pineapple', 'orange', 'mango', 'kiwi', 'apple']



As its sort numerical list

sort ascending order 

Example

    list = [ 100,50,80,90,55,20,30]

    list.sort() 

    print(list)        


Output

    [20, 30, 50, 55, 80, 90, 100]


sort descending order 

Example

    list = [ 100,50,80,90,55,20,30]

    list.sort( reverse = True ) 

    print(list)        


Output

    [100, 90, 80, 55, 50, 30, 20]


Reverse Order to print list

reverse() method is used for print the list in reverse


Example

    list = [ 100,50,80,90,55,20,30]

    list.reverse() 

    print(list)        


Output

    [30, 20, 55, 90, 80, 50, 100]


 


    

Post a Comment (0)
Previous Post Next Post