Björn Beyens Björn Beyens - 1 month ago 4
Python Question

Trying to compare every number out of this list with the 1 infront of it and the 1 after it

I'm trying to compare every number out of this list with the 1 infront of it and the 1 after it.
For example: I got a list participants = ["A:1", "B:6", "C:5", "D:4", "E:7", "F:3", "G:10", "H:2"]
Now I wanna compare the number of B:6 to A:1 and C:5, but I have no idea how since that A,B and C are still in the way and I need to keep them for later.

Answer

You can use split, int and zip. Let's name your list l:

for previous, current, next in zip(l[:-2], l[1:-1],l[2:]):
    nums = [int(x.split(':')[1]) for x in [previous, current, next]] 
    # now do your comparison... 
Comments