/* */

Tuesday 22 March 2011

Using 'key' in sort

x = ['add', 'acme', 'aerate', 'aardvark', 'abalone']
x.sort(key = len)
x
['add', 'acme', 'aerate', 'abalone', 'aardvark']

This example uses 'len' as the key function - it arranges the items in order of their length.

Sorting tutorial online:

http://wiki.python.org/moin/HowTo/Sorting 

No comments:

Post a Comment