> ## Content Index
> Fetch the complete content index at: https://new-blog.dougals.me/llms.txt
> Use this file to discover other available public pages before exploring further.

# Python List Comprehension
- URL: https://new-blog.dougals.me/python-list-comprehension/
- Published: 2022-06-13T18:44:14.000Z
- Updated: 2022-06-13T18:44:14.000Z
- Author: Dougie Richardson
- Tags: Coding, Python, #Migrated-1789219936886, #wp, #wp-post, #Import 2026-09-12 13:32

I’ve been spending time with Python recently and am beginning to really like some of the language’s features. List comprehension creates a list by evaluating an expression on each item in each list, from left to right. It combines an expression and a loop:

```bash
>>> [ord(letter) for letter in 'example']
[101, 120, 97, 109, 112, 108, 101]
```

Apply a condition :

```bash
>>> [ord(letter) for letter in 'example' if ord(letter) < 112]
[101, 97, 109, 108, 101]
```

It’s useful for combining lists:

```bash
>>> [(letter, number) for letter in 'ab' for number in '12']
[('a', '1'), ('a', '2'), ('b', '1'), ('b', '2')]
```

Pimoroni’s Rainbow Hat introduction has an example to cycle colours on the LED rainbow:

```python
for i in range(101):
    h = i / 100.0
    r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
    rh.rainbow.set_all(r, g, b)
    rh.rainbow.show()
```

This is a little hard to follow but we’ll break it down. Hue, Saturation and Value (HSV) represents colour using three values between 0.0 and 1.0 creating a colour “wheel” that is easy to cycle on the LED. Unfortunately, the LED combines red, green and blue. The Colorsys library can convert between the two.

- The expression is `int(c*255)`
- The loop is `for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)`

The `h=i/100` is giving a range of values from 0.0 to 1.0 in 0.01 steps (we could use a list comprehension too `[i/100 for i in range(101)]`).

So, let’s look at a snapshot, where h=1.3:

```bash
>>> colorsys.hsv_to_rgb(0.13,1.0,1.0)
(1.0, 0.78, 0.0)
```

Which the list comprehension converts to:

```bash
>>> [int(c*255) for c in colorsys.hsv_to_rgb(0.13, 1.0, 1.0)]
[255, 198, 0]
```

Giving us the RGB value needed.