Last Updated: Sunday 24th March 2013

Sometimes we need to generate lists which follow some natural logic, such as iterating over a sequence and applying some conditions in them. We can use Python's “list comprehension” technique to write compact codes to generate lists. We can loop through a sequence, and apply logical expression.

First, let's look at a special function range – it's used to generate a list of numbers over a range as the name suggests! Try the following code section in the Python IDLE:

So range actually generates a list. We can use it in a for loop. Look at the following example, where we generate a list named myList:

The above code snippet will have following output:

We could also create myList in one line of code using list comprehension:

Cool, huh? Let's see another example. Given a list inputList, let's create a outputList which will contain only elements of inputList having a positive index (elements at index 0, 2, 4, ...). First, let's try achieving this using a loop:

Now let's see the list comprehension version:

Both techniques will have the same output:

As a final example, given a list of integers, let's figure out the odd integers, and create a new list containing those:

Will print:

Same job to accomplish using list comprehension method will be to write something like:

That's it! Now we are in a position to describe the syntax. We start and end by third braces - “[” & “]”  - everything should be enclosed by the braces. The opening brace is followed by an expression, which is followed by a for clause and then zero or more for&if clauses.

List comprehension always returns a list, evaluating the expression we put after the opening brace.

Order of evaluating “for” clauses

If there are more than one for clauses, they will be evaluated in the exact order they get evaluated in loops. For example, note the following list comprehension,

is similar to:

Both of them give [1, 1, 2, 4, 3, 9, 4, 16] as output.

Now we’ve learned what list comprehension is, its syntax and some examples showing its usage. The question is, why use list comprehension? In many cases, you can achieve the same results without using list comprehension by using the map & lambda functions together. Note, however, that, in most cases, list comprehension is considered faster than map & lambda function put together. Besides, list comprehension makes code compact and easy to read - a must for beautiful coding. Maps also help us to code naturally; for example, we can create a list of all positive numbers using list comprehension just the way we think in our math class!

  • http://notebookcloud.appspot.com Carl Smith

    Nice article. There’s just one thing I’d change; you have the following example code:

    output = [ x for x in input if not x%2 == 0 ]
    print output

    It’s fine, but could probably be improved by using the not-equals operator instead of the keyword not being followed by an is-equals operator. You could also print the comprehension directly.

    print [ x for x in input if x%2 != 0 ]

  • http://fuckjerry.tumblr.com/ Triple OG Pimp C

    I’m confused. How does y in range(1, 3) in your final example return 4 and 16 as the last two numbers if range(1, 3) stops at 3?

    • Guest

      Disregard this comment I’m an idiot.