Archive for July, 2013

Python list comprehensions

generate list of lists

This will generate a list of multiples for each number 1 through 10 up to 100

[ [ x for x in range(100) if x % n == 0 ] for n in range(1,10)]

Flatten a list of lists

L = [[‘one’,’two’,’three’],[‘second’,’first’,’last’,’forth’]]
[ K[i] for K in L for i in range(0,len(K)) ]

 

Leave a Comment