0

How do I change a python iterator mid iteration?

For example:

for i in range(10):
    print i
    i = 5

This prints:

0
1
...
9

I would like to print:

0
5
5
5
...

EDIT: Sorry this question came out as confusing to people. I was interested in why when I try to change an iterator during a for loop the for loop ignores it on the next iteration.

Someone else has already submitted an answer that clears my confusion. range creates a list and then the for loop assigns the iterator to the next variable in the list.

6
  • 1
    If you change i in the loop it's not going to terminate. Even if it did - you are doing something conceptually wrong. Commented Aug 12, 2015 at 22:25
  • I realize this hence my output bring 5, 5, 5, ... as in repeating. My real code will only change the iterator inside of a condition so it can skip unnecessary data. Commented Aug 12, 2015 at 22:30
  • 1
    Why don't you tell us the problem you're trying to solve instead of the problem your attempted solution is causing? Commented Aug 12, 2015 at 22:30
  • You should tell us your actual problem, as @TigerhawkT3 said. Because while you can get the desired output that you show in your question with for x in ([0] + [5]*9): print x, or even just print '\n'.join(['0'] + ['5']*9), I don't think that's going to help with your actual problem :). Commented Aug 12, 2015 at 22:35
  • 1
    I deleted my comment that was attributing a different answer to Cody than the one he actually posted; whoops :P. Commented Aug 12, 2015 at 22:42

4 Answers 4

1

Following your response what you're trying to do already exists in the form of continue. It lets you skip an iteration by checking a certain condition holds:

for i in range(10):
    if i >= 5:
        continue # go to next iteration
    print i

Assuming you're using an iterator and not a list you can skip one element at a time, so a jump to a given index is irrelevant.

Sign up to request clarification or add additional context in comments.

Comments

0

The for loop is iterating over every number in the list created by range. You can change i for a given iteration, but the for loop just goes to the next number in the list. I think a while loop would be a different story, it actually uses the current value of i

Comments

0

To echo Cody Braun, see the following code and output:

for i in range(10):
    if i >= 5:
        i = 5
    print i

Output:

0
1
2
3
4
5
5
5
5
5

Comments

0

You could make a class that was iterable and modify the iteration in the for-loop:

>>> class MyRange:
...     def __init__(self, stop):
...         self.current = 0 
...         self.stop = stop
...     def __iter__(self):
...         return self
...     def next(self): # Python 3: def __next__(self)
...         if self.current >= self.stop:
...             raise StopIteration
...         else:
...             self.current += 1
...             return self.current - 1 
...
>>> for i in MyRange(3):
...     print i
...
0
1
2
>>> r = MyRange(10)
>>> for i in r:
...     print i
...     r.current += 1
...
0
2
4
6
8
>>> r = MyRange(10)
>>> for i in r:
...     print i
...     r.current = 5
0
5
5
5
5
5
# and so on...

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.