Python sort Function

Python list has a sort() method which sort the list ascendly. The sorted() function can sort list and other iterables.

>>> x = [65,3,43,90,6,5]
>>> x.sort()
>>> x
[3,5,6,43,65,90]
>>> x = [65,3,43,90,6,5]
>>> sorted(x)
[3,5,6,43,65,90]
sorted() method can also sort dictionary:
>>> x = {3: "Jeff", 98: "Lynn", 32: "Jonanson"}
>>> x
{32: 'Jonanson', 98: 'Lynn', 3: 'Jeff'}
>>> sorted(x)
[3, 32, 98]
>>> y = sorted(x)
>>> for i in y: print(i, x[i])
...
3 Jeff
32 Jonanson
98 Lynn














endmemo.com © 2024  | Terms of Use | Privacy | Home