Mar. 21, 2013

List Comprehensions in Python

List Comprehensions

List comprehensions provide a concise way to create lists. 

It consists of brackets containing an expression followed by a for clause, then
zero or more for or if clauses. The expressions can be anything, meaning you can
put in all kinds of objects in lists.

The result will be a new list resulting from evaluating the expression in the
context of the for and if clauses which follow it. 

The list comprehension always returns a result list. 
If you used to do it like this:
new_list = []
for i in old_list:
    if filter(i):
You can obtain the same thing using list comprehension:
new_list = [expression(i) for i in old_list if filter(i)]


The list comprehension starts with a '[' and ']', to help you remember that the
result is going to be a list.

The basic syntax is
[ expression for item in list if conditional ]
This is equivalent to:
for item in list:
    if conditional:
Let's break this down and see what it does
new_list = [expression(i) for i in old_list if filter(i)]
The new list (result).

Expression is based on the variable used for each element in the old list.

for i in old_list
The word for followed by the variable name to use, followed by the word in the
old list.

if filter(i)
Apply a filter with an If-statement.
This blog shows an example of how to visually break down the list comprehension:

new_range  = [i * i          for i in range(5)   if i % 2 == 0]

Which corresponds to:

*result*  = [*transform*    *iteration*         *filter*     ]

The * operator is used to repeat. The filter part answers the question if the
item should be transformed. 


Now when we know the syntax of list comprehensions, let's show some examples and
how you can use it. 
Create a simple list
Let's start easy by creating a simple list.
x = [i for i in range(10)]
print x

# This will give the output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
That's how you can create a simple list.
Create a list using loops and list comprehension
For the next example, assume we want to create a list of squares.

# You can either use loops:
squares = []

for x in range(10):
print squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Or you can use list comprehensions to get the same result:
squares = [x**2 for x in range(10)]

print squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Just remember the syntax: [ expression for item in list if conditional ]
Multiplying parts of a list.
Multiply every part of a list by three and assign it to a new list.

list1 = [3,4,5]
multiplied = [item*3 for item in list1] 
print multiplied 
Note how the item*3 multiplies each piece by 3.
Show the first letter of each word
We will take the first letter of each word and make a list out of it.

listOfWords = ["this","is","a","list","of","words"]

items = [ word[0] for word in listOfWords ]

print items
The output should be: ['t', 'i', 'a', 'l', 'o', 'w']
Lower/Upper case converter
Let's show how easy you can convert lower case / upper case letters.

>>> [x.lower() for x in ["A","B","C"]]
['a', 'b', 'c']

>>> [x.upper() for x in ["a","b","c"]]
['A', 'B', 'C']
Print numbers only from a given string
This example show how to extract all the numbers from a string.

string = "Hello 12345 World"
numbers = [x for x in string if x.isdigit()]
print numbers

>> ['1', '2', '3', '4', '5']
Change x.isdigit() to x.isalpha() if you don't want any numbers.
Parsing a file using list comprehension
In this example, we can see how to get specific lines out from a text file. 

Create a text file and put in some text in it. 

this is line1
this is line2
this is line3
this is line4
this is line5

Save the file as test.txt

# Then create the filter by using list comprehension:

fh = open("test.txt", "r")

result = [i for i in fh if "line3" in i]

print result
Output: ['this is line3
Using list comprehension in functions
Now, let's see how we can use list comprehension in functions.
# Create a function and name it double:
def double(x):
  return x*2

# If you now just print that function with a value in it, it should look like this:
>>> print double(10)
We can easily use list comprehension on that function.

>>> [double(x) for x in range(10)]

print double
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# You can put in conditions:

>>> [double(x) for x in range(10) if x%2==0]
[0, 4, 8, 12, 16]

# You can add more arguments:

>>> [x+y for x in [10,30,50] for y in [20,40,60]]
[30, 50, 70, 50, 70, 90, 70, 90, 110]
See how you can put in conditions and add more arguments.

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, only recommend products or services that we try personally and believe will add value to our readers.