Multilevel list comprehensions in Python

August 1, 2005

Python has recently (at least for some values of recently) grown 'list comprehensions', which let you easily iterate over a list to transform or select entries (or both). List comprehensions can be thought of as syntactic sugar for map and filter operations, but they're actually more powerful.

One reason is that you can write a multilevel list comprehension, which effectively iterates over multiple levels of lists. Take the case where you have a list within a list and want to return all of the low-level elements as a list:

l = []
for rr in qa:
  for s in rr.strings:

This can be rewritten as a two-level list comprehension:

l = [s for rr in qa for s in rr.strings]

This can't easily be done via map. (We would probably have to roll in a reduce to flatten the list of lists that map would give us into a single-level list.)

Multilevel list comprehensions work left to right; the leftmost 'for X in Y' is the outermost one, and then we step inwards as we move right. You can also use if conditions, so the correct version of the list comprehension I wrote, in context and with error checking, would be:

from dns.rdatatype import TXT
l = [s for rr in qa if rr.rdtype == TXT \
     for s in rr.strings]

What impresses me about Python is that this works just the way I thought it would work and both of these examples worked the first time, just as I wrote them, and needed no debugging. (The first version actually got used in a scratch program.)

Written on 01 August 2005.
« Spam breakdown by SBL listing, July 31st 2005
Why Perl is not my favorite language »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Aug 1 23:41:47 2005
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.