List comprehensions can help your programming

More *sometimes* better

By Cameron Laird  Add a new comment

'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):
            list_that_matters.append(f2(item))

followed by sorting list_that_matters, 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 if-s and print-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.

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question