List comprehensions can help your programming

More *sometimes* better


'Know what a "list comprehension" is?

Odds are, you do--but that's a different thing from using it, and using it well might be in a third category. Let's look at a couple of specific examples, and see what they teach.

Modern history

Code for this example will be in Python; I was reading Python sources when I began this note. Lots of modern languages have comprehensions (sometimes list comprehensions, but also comprehensions over sets, dictionaries, generators, and other types), especially those from the functional wing. The first widely-used language to use "comprehension" for the construct was Miranda.

This is intriguing, as Python has generally distanced itself from functional styles in recent years. Python author Guido van Rossum has quite deliberately chosen to incorporate more than one kind of comprehension in Python, though, at the same time as he strives to reduce the complexity of the language in other ways. What's the big deal?

My own experience chatting with programmers is that many confidently explain comprehensions with an example such a

[x * x for x in range(10)]
: "that lists the first ten squares,
[0, 1, 4, ..., 81]
". They're right.

What's the point of that, though? Many seem to regard it as an academic footnote, no more practical than, well, a list of squares.

On another hand, how often have you done something like this?

    big_list = f1()
    list_that_matters = []
    for item in big_list:
        if important_today(item):

followed by sorting

, or passing it to another function, or so on. Since 2.0 of Python, however, you can also write this as
list_that_matters = [f2(item) for item in f1() if important_today(item)]
. Is the latter better? Fitting on a single line isn't a win if it hurts readability; in this case, though, yes, I think the list comprehension is generally likely to get the point across more effectively to the maintainer who next picks up this fragment two years in the future. Moreover, as piman explains in more detail, list comprehensions also have the potential to help with performance.

A matter of style

I just came across an example "in the wild" that made me think of the expressivity of comprehensions. The source code, as it reached me, was something I'll simplify for ease in explanation as

    if status == "status1":
        print '<a href = "link1">' + explanation1 + '</a>'
        print '<br />'
        print '<a href = "link2">' + explanation2 + '</a>'
        print '<br />'
    if status == "status2":
        print '<a href = "link2">' + explanation2 + '</a>'
    if ...

The code went on for pages. Here's one reduction I made (again, approximately--I leave aside certain issues not pertinent to this discussion):

    description2 = ("link2", "explanation2")
    description_list['status1'] = [('link1', 'explanation1'), description2, ...]
    description_list['status2'] = [description2]
    print "<br />".join(["<a href = '%s'>%s</a>" % pair for pair in description_list[status]])

Do I truly think that rather obscure one-liner is easier to read than the utterly elementary sequence of

-s and
-s? Yes: in the first place, the change reduced the line-count by about half. More telling, a handful of typographical errors were actually present in the original, but no one had noticed in all the lines of quotes and other punctuation. Now the hyperlink formula appears in just one place, and it's far easier to correct.

My conclusion: comprehensions are good for more than just a couple of extra points on a test.

Take it farther

One of the great features of Python, and coincidentally JavaScript and XPath, two other languages which support comprehensions, is that you can start useful coding with a small investment: you don't have to learn all of Python to begin to code effectively. Don't mistake that, though, for an excuse to stop learning. While nearly all languages have obscure features, best left to experts and/or rare situations, comprehensions aren't among them. Comprehensions are good ways to express code that arises in many common situations. Be on the look-out for opportunities where they can help you.

Van Rossum wrote about functional influences on Python earlier this year.

Now Read This: IT Resume Makeover: Our top 11 tips
View Comments
You Might Like
Join the discussion
Be the first to comment on this article. Our Commenting Policies