Thursday, May 28, 2009

This is why we love python!

Well, this is what Kushal said, and I agree completely.

I wanted to sort a dictionary by value. I asked Kushal.
He said, "I don't know. You can sort it by key. Maybe you have to do manually!"

I went ahead and googled...

And this is how we do it:

alist = sorted(alist.iteritems(), key=lambda (k,v): (v,k))

2 comments:

Harsha said...

what is the usage context of such a feature?

$hRiNk said...

you can always sort a dictionary by key, I wanted to sort it by values.