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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s