12

So I wrote a for loop like this:

for i in range(size):
  if(.....)
    ....
    i-=1
  else:
    ....

I try to decrease the index by 1 if it is inside the if statement, but apparently I can't do that. Is there any other way that I can decrease i in a for loop?

5
  • Do not use i or it'll run forever as you try to decrease the i and range() increases it. Commented Feb 21, 2015 at 19:15
  • If you would like constructive help, please post some real python code instead of some pseudo gibberish. Commented Feb 21, 2015 at 19:16
  • 1
    @ForceBru That is not how range() works. range() merely creates a list and the for <var> in <iterable> construction runs the loop assigning <var> each value from the <iterable>. If <var> is changed inside the loop, it has no bearing on the next iteration. Commented Feb 21, 2015 at 19:20
  • @SH.C: Hi. Welcome to SO. The question you have posted is a bit underspecified (which may explain the down-votes). We need some more information about what you want to do. If you are not tied to using for loops, then it seems that a while loop may suit your use-case better. Commented Feb 21, 2015 at 19:24
  • 1
    @musically_ut: Sorry about the confusion, It's my first time use this website. I think I already got the answer.You are right, I should use a while loop instead of for loop. Commented Feb 21, 2015 at 19:50

5 Answers 5

18

I would like to go over the range() function yet again through the documentation as provided here: Python 3.4.1 Documentation for range(start, stop[, step])

As shown in the documentation above, you may enter three parameters for the range function 'start', 'stop', and 'step', and in the end it will give you an immutable sequence.

The 'start' parameter defines when the counter variable for your case 'i' should begin at. The 'end' parameter is essentially what the size parameter does in your case. And as for what you want, being that you wish to decrease the variable 'i' by 1 every loop, you can have the parameter 'step' be -1 which signifies that on each iteration of the for loop, the variable 'i' will go down by 1.

You can set the 'step' to be -2 or -4 as well, which would make the for loop count 'i' down on every increment 2 down or 4 down respectively.

Example:

for x in range(9, 3, -3):
    print(x)

Prints out: 9, 6. It starts at 9, ends at 3, and steps down by a counter of 3. By the time it reaches 3, it will stop and hence why '3' itself is not printed.

EDIT: Just noticed the fact that it appears you may want to decrease the 'i' value in the for loop...? What I would do for that then is simply have a while loop instead where you would have a variable exposed that you can modify at your whim.

test = 0
size = 50
while (test < size)
    test += 1
    if (some condition):
        test -= 1
Sign up to request clarification or add additional context in comments.

Comments

6

For such a construct, where you need to vary the loop index, a for loop might not be the best option.

for <var> in <iterable> merely runs the loop body with the <var> taking each value available from the iterable. Changing the <var> will have no effect on the ensuing iterations.

However, since it is usually tricky to work out the sequence of values i will take beforehand, you can use a while loop.

i = 0
while i < size:
    if condition:
        ...
        i -= 1
    else:
        ...
        i += 1

Comments

0

You should not mutate i. The for i in range(size): is kind of special. Instead you should probably use a list comprehension:

size_range = [x-1 if condition else x for x in range(size)]

Actually probably better:

def f(x):
    ... #Do else stuff here.

decrement = lambda x: x-1
size_range = [decrement(x) if condition else f(x) for x in range(size)]

Comments

0

Here you can find some more examples on range

But what you probably want is something like this: for i in range(size, 0):.

Comments

0

for decrement operator in for loop is

for i in range(5,1,-1): print(i)

it will deceremet by one prints 5432

     for increment index last parameter should be in positive
     for decrement index last parameter should be in negative 

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.