Categories
Planet Python

How to love python list comprehensions ?

I wanted to generate a list of 640 x 480 2d points. The format of the list must be (x1, y1, x2, y2 … xn, yn).
I just came with a solution using Python list comprehensions :

w, h = 640, 480
mesh = [0] * w * h * 2
mesh[0::2] = range(w) * h
mesh[1::2] = (x for x in xrange(h) for m in xrange(w))

Seriously, thanks python.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.