1

In a typical C-like language a for loop gives you more control over iteration. I'm wondering how to do the equivilent

for(int i = 0; i < A.length; i++) {
  do_things(A[i]);
  if (is_true(i)) {
    i--;
  }
}

in Python?

In other languages I opt to use their container based for loop constructs, but they typically have vanilla for loops that I can use for situations like this. How do I get more "control" when iterating in Python?

I imagine the answer to this is very basic, but the search terms are muddied with other questions.

8
  • 1
    You have to use the equivalent while-loop, or write an iterator with this capability. Commented Aug 24, 2017 at 23:53
  • I recommend that you play with the Python iteration capabilities. As many people have learned, you can alter the iteration variable within the loop. However, evaluation of array size and other side effects may surprise you, as Python has some different optimizations and variable handling. Commented Aug 24, 2017 at 23:56
  • 1
    Also, please note that making such changes in the iteration parameter is a dangerous practice. In the development groups I've joined over the past 10-15 years, the code you posted would fail review. Commented Aug 24, 2017 at 23:58
  • 1
    Your example doesn't stop the iteration, that's done with a break statement, which exists in Python. Commented Aug 24, 2017 at 23:59
  • 1
    @2c2c Use continue instead then. Decrementing i is the wrong action. Commented Aug 25, 2017 at 0:17

1 Answer 1

1

The best equivalent in Python would be a while-loop:

i = 0
while i < A.length: # If `A` is a regular Python container type, use `len()`
    do_things(A[i])
    if  is_true(i):
        i -= 1
    i += 1

Note however, as said in the comments, iterating like this over a container is more often than not, a bad idea. You should review your code and make sure you actually need this behavior.


EDIT

It's just a sloppy explanation of a loop that doesnt continue to the next element until some condition is met. I didn't explain clear enough I guess

Use continue instead then. Decrementing i is the wrong action:

i = 0
while i < A.length:
    do_things(A[i])
    if not is_true(i):
        continue
    i += 1

Better yet, you can ditch the while-loop all together and use enumerate:

for i, el in enumerate(A):
    if not is_true(el):
        continue
    # do work
Sign up to request clarification or add additional context in comments.

2 Comments

A.length won't usually work (too much Javascript?). You probably meant len(A) :-)
@mgilson Well. not exactly. I wasn't exact;y sure what A was. Whether it was a custom type, or a simple array. I opted to keep accuracy to the original code while translating, but your probably right. So I'll ad a note.

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.