Thursday, January 1, 2015

A Beginners Guide to List Comprehensions

Most people think of list comprehensions as being a complex and unnecessary  part of Python. However, with a basic understanding of list comprehensions, many things that would take several lines of code to do take only one or two! If this short intro has convinced you that you need to learn about list comprehensions, read on!


I'm going to start by showing you a simple list comprehension and then I will explain how it works. Here it is:
l1 = [x + 1 for x in l] 
List comprehensions may look complicated, but what is going on here is actually fairly simple...It is easier to see if I show you the same code without using list comprehensions: 
l1 = []
for x in l:
    l1.append(x + 1)
Now what's happening becomes much more obvious. A list comprehension is a shortcut of generating a new list by using data from other lists. In both pieces of code above, l is the list we are using to create a new one, l1 is the new list, and x is a temporary variable that is used to create the new list. Now basically what happens, is the list comprehension goes through every item in l and, for each one(represented by x), adds one to it(hence x + 1) and puts it in the new list...It's that simple. In this case if l = [1, 2, 3, 4, 5, 6, 7, 8, 9], then l1 = [2, 3, 4, 5, 6, 7, 8, 9, 10]. If you didn't quite grasp it, don't worry, just read through this a few more times and see if you get it, then move on to the next part.

List comprehensions can also be more complicated. They can include multiple nested for loops, conditions, and more complex expressions(they're not just limited to x + 1). Don't worry, we'll go through all of these in turn.

First are multiple nested loops. To understand these, take the example where you have a two-dimensional list such as this:
l = [
     [1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]
    ]
Then we wanted to take that list and generate a one-dimensional list of all the elements, so the result is something like this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
This could be done in one line with a list comprehension like this:
l1 = [x for i in l for x in i]
The corresponding non-comprehension code is:
l1 = []
for i in l:
    for x in i:
        l1.append(x)
This comprehension is slightly more complicated but the essential idea is the same. If you don't understand this, again read through the last section and try out the code yourself. 

The next advanced comprehension topic is conditions. Conditions allow you to only include some of the elements in the new list. For example, let's say you have this list:
l = [1, 0, 4, 56, 78, -2, 6, -84, 67, -299, 356]
You want to create a new list containing all the elements in this list that are more than zero. Code:
l1 = [x for x in l if x > 0]
Here is the same thing done without comprehensions:
l1 = []
for x in l:
    if x > 0:
        l1.append(x)
Notice if statement? that is what does all the magic. Basically, this code goes through all the elements in l and, for each one, if it's more than zero, adds it to l1. The resulting value of l1 is:
[1, 4, 56, 78, 6, 67, 356]
If you understand this more or less... Congratulations! If not, once again go back and re-read, test, and keep going.

The last complex topic is actually very simple...complex expressions. Basically, you can have expressions as simple as x, or as complex as (x ** 2) + ((x - 3) * (1000 + x) and beyond.

If you have gotten to here without an information overload, Congratulations! If not, don't worry. List comprehensions can be a bit difficult to pick up right away. But I guarantee you, that before long, you will be a List Comprehension Master of the highest order.

Finally, for more information on all the things you can do with list comprehensions, check out https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions for Python 2, and https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions for Python 3.