Jun 13, 2013

Sorting The List Elements in Python

The list elements can be sorted using sort() method or sorted() function. This code shows you the difference between list's sort() method and sorted() function.
>>> # using sort() method
>>> li = [99, 28, 45, 30, 40]
>>> li.sort()
>>> li
[28, 30, 40, 45, 99]
>>> 
>>> # using sorted() function
>>> originalList = [99, 28, 45, 30, 40]
>>> sortedList = sorted(originalList)
>>> sortedList
[28, 30, 40, 45, 99]
>>> originalList
[99, 28, 45, 30, 40]
>>>

No comments:

Post a Comment