20090119

Sorting a dictionary by value

In Python you sometimes need to sort a dictionary based on its values, not the keys, as the built-in sorted function does:


>>> a = { 'a' : 1, 'b' : 3, 'd' : 2, 'c' : 4 }

>>> sorted (a)

['a', 'b', 'c', 'd']


Not exactly the output we wished for. This is because dictionary iterators iterate over the keys and not the values.


There are several possible solutions to this problem, the simplest is to add specific key parameter to sorted:


>>> sorted (a, key=(lambda x: a[x]))

['a', 'd', 'b', 'c']


This way you can iterate over the dictionary in the order of preference:


>>> for i in sorted (a, key=(lambda x: a[x])):

... print a[i]

...

1

2

3

4


Note that if you try to construct a dictionary from the sorted list it will not yield a sorted dictionary:


>>> dict ([(x, a[x]) for x in sorted (a, key=(lambda z: a[z]))])

{'a': 1, 'c': 4, 'b': 3, 'd': 2}


Dictionaries uses hashes, so the order is not guaranteed. This is why you have to construct a temporary list with the sorted keys to iterate over the dictionary in the desired order (in this case sorted by the value). In real world scenarios you sometimes have a dictionary of dictionaries, and this way you can choose the parameter you iterate over.


20090116

Ternary operator in Python

People coming from C-style languages such as PHP and C++ often wonder why there is no ternary operator (the weird x ? y : z construct) in Python.


This operator is usually used in assigments, for example


someval = (condition() ? value1 : value2);


Where someval's value depends on the return value of the function named condition(). If condition returns a value that evaluates to true, then someval will be set to value1, if false then value2.


The fact is that there is a way in Python that lets you use this kind of assignments. The trick is to use the conditional operators.


Here's an example:


>>> condition = True
>>> x = condition and 'is true' or 'is false'
>>> print 'condition %s' % x
condition is true
>>> condition = False
>>> x = condition and 'is true' or 'is false'
>>> print 'condition %s' % x
condition is false


The rule that enables this is that the return value of a logical operation (if true) is always the last value. This guarantees that x will always get a value.


Of course in C-style languages there are things one can do with the ternary operator that would fail in Python, but with logical operators the most typical use of it can be simulated, and it's even a bit more readable for people who are not very familiar with the language. I would even go as far as saying it's syntax is superior to the ? : notation.