# Revision history [back]

List comprehension is a way of constructing lists that is very readable and concise. It mimics set builder notation from mathematics:

f(x,y) = x + y
L = [ (a, b, f(a, b)) for (a,b) in [ (0,0), (1,1), (5.6, 7.0) ] ]


or

L = [ (a, b, f(a, b)) for a in range(10) for b in range(10) ]


try these out and see what they produce.

You can now modify your list or add to it any way you want. For example:

f(x,y) = x + y
L = [ (a, b, f(a, b)) for (a,b) in [ (0,0), (1,1), (5.6, 7.0) ] ]
print L[0] # prints (0,0,0)
L[0] = (1,2, f(1,2))
print L[0] # prints (1,2,3)

L.append((1.1, 1.2, f(1.1, 1.2)))
print L[3]