Python Tutorial – List Comprehensions

PythonThis Python Tutorial post will introduce a very cool technique for Object Oriented guy like me: List Comprehensions. It provides way to create new lists while doing some operations to elements in the list.

List Comprehensions

Lets take a simple example how you would create a list of numbers in a range multiplied with two. Quite school example, but hang on.

values = []
for number in range(5):
    values.append(number * 2)

print values
""" Prints: [0, 2, 4, 6, 8] """

This gets the job done. For object oriented guy this would be standard way to do this. There’s luckily shorter way to do same with comprehension.

print [number * 2 for number in range(5)]
""" Prints: [0, 2, 4, 6, 8] """

Inside brackets first part is expression and after that there is for clause. Comprehension can has an optional part that can contain zero or more for and if clauses.

Here is an example of if clause in list comprehension.

print [number * 2 for number in range(5) if number % 2 == 0]
""" Prints: [0, 4, 8] """

Now comes the part that things get a bit ugly. Here are two versions of flattening loops.

numbers = [
    [12,13,14],
    [15,16,17],
    [18,19,20],

]
print [number for row in numbers for number in row]
""" Prints[12, 13, 14, 15, 16, 17, 18, 19, 20] """

all = []
for row in numbers:
    for number in row:
        all.append(number)

print all
""" Prints[12, 13, 14, 15, 16, 17, 18, 19, 20] """

I find the first version completely unusable because it looks very messy. I would gladly use latter version because you can understand it with one glimpse.

Conclusion

I started this post with saying that List Comprehensions are cool. They still are and somewhat usable way to shorten your code.
But.

List Comprehension comes with a price. They are almost always harder to read than their clearer versions. Think twice before writing something just for less lines of code.

This entry was posted in Programming, Python and tagged , , , . Bookmark the permalink.

4 Responses to Python Tutorial – List Comprehensions

  1. Auramo says:

    I think you are missing a core concept here: functional programming. And with it, immutability. I agree that the syntax of Python’s list comprehensions might not be ideal. Just saying that nested loops are always easier to reason about than multiple expressions in a list comprehension still rubs me the wrong way.

    With functional constructs you don’t (or at least shouldn’t) mutate anything, you get new collections back. With imperative nested loops which change the guts of things all over the place it gets hard to see what’s going on very quickly.

    Also, I think multiple expressions in comprehensions are more readable if you put them in separate lines:

    new_list = [number for row in numbers
    for number in row]

  2. Jari Timonen says:

    Indentation makes it a bit readable, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>