8

Is it possible to increment a for loop inside of the loop in python 3?

for example:

for i in range(0, len(foo_list)):
    if foo_list[i] < bar
        i += 4

Where the loop counter i gets incremented by 4 if the condition holds true, else it will just increment by one (or whatever the step value is for the for loop)?

I know a while loop would be more applicable for an application like this, but it would be good to know if this (or something like this) in a for loop is possible.

Thanks!

1
  • 1
    this might be an x y problem: im not sure why you wouldnt just process your list better before you iterate over it. also look at this: for i,item_in_list in enumerate(foo_list): print(i,item_in_list) Commented Oct 16, 2014 at 19:14

5 Answers 5

12

You could use a while loop and increment i based on the condition:

while i < (len(foo_list)): 
    if foo_list[i] < bar: # if condition is True increment by 4
        i += 4
    else: 
        i += 1 # else just increment 1 by one and check next `foo_list[i]`

Using a for loop i will always return to the next value in the range:

foo_list = [1,2,3,4,5,6]
bar = 6
for i in range(len(foo_list)):
    print("range i ",i)
    if foo_list[i] < bar:
        i += 4
        print("if i",i)


('range i ', 0)
('if i', 4)
('range i ', 1)
('if i', 5)
('range i ', 2)
('if i', 6)
('range i ', 3)
('if i', 7)
('range i ', 4)
('if i', 8)
('range i ', 5)
Sign up to request clarification or add additional context in comments.

1 Comment

I know my example code doesn't work the way it's supposed to. I was trying to illustrate what I meant by incrementing the loop by more than 1. Thanks for your response though!
2

a bit hackish...

>>> b = iter(range(10))
>>> for i in b:
...     print(i)
...     if i==5 : i = next(b)
... 
0
1
2
3
4
5
7
8
9
>>> 

1 Comment

This can be used to replace a while loop with a for loop
1

In your example as written i will be reset at each new iteration of the loop (which may seem a little counterintuitive), as seen here:

foo_list = [1, 2, 3]

for i in range(len(foo_list)):
    print('Before increment:', i)
    i += 4
    print('After increment', i)

>>>
Before increment: 0
After increment 4
Before increment: 1
After increment 5
Before increment: 2
After increment 6

continue is the standard/safe way to skip to the next single iteration of a loop, but it would be far more awkward to chain continues together than to just use a while loop as others suggested.

Comments

1

It is possible to achieve this with a for loop using the send method of a generator, where the counter can be modified if the generator is sent a new value:

def jumpable_range(end):
    counter = 0
    while counter < end:
        jump = yield counter
        if jump is None:
            counter += 1
        else:
            counter = jump

foo_list = [1, 2, 3, 4, 5, 6, 7, 8]
bar = 6
iterable = jumpable_range(len(foo_list))
for i in iterable:
    if foo_list[i] < bar:
        i = iterable.send(i + 4)
    print(i)

This outputs:

4
5
6
7

Demo: https://repl.it/@blhsing/ImpossibleAcrobaticComputergraphics

Comments

0
while i < end:
  # do stuff
  # maybe i +=1
  # or maybe i += 4

I suppose you could do this in a for loop if you tried, but it's not advisable. The whole point of python's for loop is to look at items, not indices

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.