Python list comprehensions was Re: python and 2D lists

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: "Programming" <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 2 Oct 2009 12:15:59 -0700

Hi Jim,

List comprehensions arre a very cool way to build a list in Python.

Building a list of items is a common programming idiom.  In Python the
basic approach using for loops is something like the following code:

itemList = [] # create an empty list
for item in sequence: # iterate through each item in the sequence
 itemList.append(item) # add each item to the itemList

To do the same thing in a single statement, use the list comprehension:

itemList = [item for item in sequence]

That is the basic form, but as usual considerable complexity can always be added for more involved situations. I will try to explain below.

First, a list comprehension is an expression. As such it can be used anywhere an expression can be used for example 1) in an assignment statement, 2) as a function call argument, 3) as a function return value. The most common is probably in an assignment statement:

myList = [i for i in "abc"]

Creates the list:
['a', 'b', 'c']

The general form of the list comprehension statement is:
[expression for target in iterable clauses]

expression must be enclosed in parens if a tuple
target and iterable are the same as a normal for statement
clauses are zero or more statements of the following form:
1) for target in iterable
2) if expression

Now look at using a more complex expression:
[i+i for i in 'abc']
gives:
['aa', 'bb', 'cc']

Note you may also call a function for the expression
def double(x): # simple double function
 return x+x
list comprehension:
[double(i) for i in 'abc']
gives:
['aa', 'bb', 'cc']

Now look at two loops:
[i+j for i in 'ab' for j in '12']
gives:
['a1', 'a2', 'b1', 'b2']
Note the loops nest in the same order as normal code.

Now try an if statement:
[i for i in 'abcde' if i != 'c']
gives:
['a', 'b', 'd', 'e']

As far as I know, you may arbitrarily add any number of loops and if statements, but no else clauses.

Finally, since a list comprehension is an expression, a list comprehension may be the expression within another list comprehension. For example:
[[i+j for i in 'ab'] for j in '12']
gives:
[['a1', 'b1'], ['a2', 'b2']]
Note the letters are the inner loop here because it is in the inner list.

List comprehensions execute faster than the equivalent for loop code structures and are syntactically cleaner once you get used to them.

That is about all I can think of off the top of my head. If you have further questions, let me know. Note I did not compile any code here so there may be some typos.

Richard
----- Original Message ----- From: "Homme, James" <james.homme@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Friday, October 02, 2009 4:14 AM
Subject: RE: python and 2D lists


Hi Richard,
List comprehensions.

Thanks.

Jim

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts:

  • » Python list comprehensions was Re: python and 2D lists - Richard Dinger