Friday, 4 December 2015

Python: list: sort elements of list

‘list’ provides sort method to sort elements of list. By default, sort method sort elements in ascending order. To sort elements in descending order, pass ‘reverse=True’ argument.

test.py

list=[12, 5, 32, 43, 21, 356]
print(list)

list.sort()
print(list)

list.sort(reverse=True)
print(list)

$ python3 test.py
[12, 5, 32, 43, 21, 356]
[5, 12, 21, 32, 43, 356]
[356, 43, 32, 21, 12, 5]




Previous                                                 Next                                                 Home

No comments:

Post a Comment