Python: Best way to frame a loop with 2 alternating tasks -
i have list of items need process. easy enough: simple for x in list
.
however, commands inside loop need different, on alternating basis. n = 0
handled 1 way, n = 1
different way; n = 2
same n = 0
. note n
, x
different!
at moment, way can think of doing incremental counter , if statements. i'm presuming there's easier way?
hope makes sense. thanks
you use enumerate
, modulo alternate:
li = ['a', 'b', 'c'] idx, ch in enumerate(li): if idx % 2 == 0: # else: # b
Comments
Post a Comment