As Tordek says, you can use items() or iteritems() in this case to avoid the problem:
colprint(sorted((name, packages[0].summary or '') for (name, packages) in versions.items()))
Moving sorting outside is a nice touch.
[Note that using items() slightly changed the sort order - it was used by name with first-order relationships (Python sorting is stable), now it is by name with relationships resolved by summary. Since the original dict order is random, the new behavior is probably better.]
But for other purposes (for example, the example of Alex Martelli) a "let" -alike may be useful. I also once discovered a trick for var in [value] , but now I find it ugly. A cleaner alternative could be a “pipeline” of concepts / generators using the “decorate / undecorate” trick to pass the added value to the tuple:
Or applied to Alex's example:
ys = (somefun(z) for z in zs) xs = [(y, y*1.2, y-3.4) for y in ys]
(in which you don't even need the original z values, so intermediate values don't have to be tuples.)
See http://www.dabeaz.com/generators/ for more powerful examples of the pipeline technique ...
Beni cherniavsky-paskin
source share