Refactoring Nested For Loops to List Comprehensions

Playing around with some more python code for the microbit, I noticed when I typically need to write to individual pixels on the LED matrix display, I default to a set of two nested “for” loops. That always feels kind of wrong to me and I wondered how the code would look using a list comprehension as micro python supports that construct.

First the original version…


for x in range(0, 5):
    for y in range(0, 5):
        display.set_pixel(x, y, 9)

and using the list comprehension syntax. We compress the two for loops into a single generator to create a list of x, y pairs. In this case, it generates it so that it works from top left (0, 0) across the top row (4, 0) then down to the next row and so on down to (4, 4) in the bottom right.


leds = [(x, y) for y in range(0, 5) for x in range(0, 5) ]

for led in leds:
    display.set_pixel(led[0], led[1], 9)

To work with y values going down the columns first, you can reverse the order of the two for pieces, still working from top left to bottom right but in the opposite sense.


leds = [(x, y) for x in range(0, 5) for y in range(0, 5) ]

for led in leds:
    display.set_pixel(led[0], led[1], 9)

So slightly more compact, one for loop to traverse when we are doing stuff to the leds and that list can be reused for each time we want to iterate through the leds in the display.