Sorting in python

One of the strong python feature is its data structure,list and dictionary give you the power to play with your data in versatile way.
Also have some methods which are needed by python programmer in day to day code work.

One of the basic and strong method is sorting.
Example is:


>>>a = [1,2,3,6,3,4]
>>>a.sort()
>>>print a
[1, 2, 3, 3, 4, 6]

Another efficient feature that we can use key while doing the sorting.Its like:


>>> a = [1,2,3,4]
>>> sorted(a, reverse=True)
[4, 3, 2, 1]

Another parameter we can use during sorted() is “key”


>>>a = ['e','f','a','g']
>>> sorted(a, key=str.lower)
['a', 'e', 'f', 'g']

Advertisements