# Lists in Python

**URL:** <https://forums.pimoroni.com/t/lists-in-python/19899>\
**Category:** Support\
**Created:** [July 24, 2022, 3:35pm UTC](https://forums.pimoroni.com/t/lists-in-python/19899 "2022-07-24T15:35:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mickf](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mickf](https://forums.pimoroni.com/u/mickf)\
**Post date:** [July 24, 2022, 3:35pm UTC](https://forums.pimoroni.com/t/lists-in-python/19899/1 "2022-07-24T15:35:15Z")

</div>

I have a project using the Plasma 2040 programmed in Python. I have a Python list of 8 items which I wish to step through sequentially for each press of the “Boot” button but to then return to the first item when I reach the last in the list and start again, “ad infinitum” (or at least until I switch Off!). Any thoughts?

---

<div class="post-metadata">

**Author:** ![Tonygo2](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/tonygo2/32/7814_2.png) [@Tonygo2](https://forums.pimoroni.com/u/Tonygo2)\
**Post date:** [July 25, 2022, 10:12am UTC](https://forums.pimoroni.com/t/lists-in-python/19899/2 "2022-07-25T10:12:31Z")

</div>

Demo below on a method using Pico Display V1 and button A

```auto
import time
from pimoroni import Button
button_a = Button(12)

list = ["a","b","c","d","e","f","g","h"]

p = -1
while True:
    if button_a.read():
        p = (p + 1) % 8
        print(p,list[p])

```

Produces this:

```auto
MicroPython 99c2589 on 2022-07-07; Raspberry Pi Pico W with RP2040

Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
0 a
1 b
2 c
3 d
4 e
5 f

```

Hope this is what you wanted.

---

<div class="post-metadata">

**Author:** ![mickf](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mickf](https://forums.pimoroni.com/u/mickf)\
**Post date:** [July 25, 2022, 10:37am UTC](https://forums.pimoroni.com/t/lists-in-python/19899/3 "2022-07-25T10:37:09Z")

</div>

Thank you Tony for a very prompt response and I think this will get me where I want to be. Sorry if it seemed to be a “Noddy” problem but I am still learning Python and have been scratching my head over this for a few days. Thanks again!  
Mick Farmer

---

<div class="post-metadata">

**Author:** ![Shoe](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Shoe](https://forums.pimoroni.com/u/Shoe)\
**Post date:** [July 25, 2022, 12:52pm UTC](https://forums.pimoroni.com/t/lists-in-python/19899/4 "2022-07-25T12:52:59Z")

</div>

> p = (p + 1) % 8

Huh, I’d made a mental note to reply to this and had an `if p == 8, p = 0` loop in mind for the roll-over, but using `% 8` is a super-elegant way to do that. You learn something every day!

---

<div class="post-metadata">

**Author:** ![Tonygo2](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/tonygo2/32/7814_2.png) [@Tonygo2](https://forums.pimoroni.com/u/Tonygo2)\
**Post date:** [July 25, 2022, 12:58pm UTC](https://forums.pimoroni.com/t/lists-in-python/19899/5 "2022-07-25T12:58:46Z")

</div>

I only twigged this myself a couple of weeks ago while working on a Neopixel ring and I’ve been coding since 1968!  
The beauty of it is that it works in both directions (+1 and -1).
