4

How do you control the index of a python for loop? (or can you? or should you?)

For Example:

for i in range(10):
    print i
    i = i + 1

Yields:

0
1
2
3
4
5
6
7
8
9

I want it to yield:

0
2
3
4
5
6
7
8
9
10

I really do apologize if I'm just completely off track with this question, and my brain is completely failing me at the moment and the solution is obvious.


Why am I asking?

This is irrelevant to the question, but relevant to the why I need the answer.

In a Python script I'm writing, I am doing something like this:

for i in persons:
    for j in persons[-1(len(persons) - i - 1:]:
        if j.name in i.name:
            #remove j.name
        else: 
            #remove i.name

    #For every person (i), iterate trough every other person (j) after person (i)
    #The reason I ask this question is because sometimes I will remove person i.
    #When that happens, the index still increases and jumps over the person after i
    #So I want to decrement the index so I don't skip over that person.

Maybe I am going about this completely the wrong way, maybe I should use a while loop and control my indices.

Jordan Carroll
  • 696
  • 2
  • 11
  • 29
  • See http://stackoverflow.com/questions/2990121/how-do-i-loop-through-a-python-list-by-twos – Chris Mar 29 '13 at 16:34
  • it looks like you are actually wanting to do set operations on your lists. Have you looked at sets? – cmd Mar 29 '13 at 16:42
  • @Chris That doesn't really apply in this case, thanks. – Jordan Carroll Mar 29 '13 at 16:44
  • @cmd I think a while loop will be the way to go. – Jordan Carroll Mar 29 '13 at 16:45
  • "The reason I ask this question is because sometimes I will remove person i." So you're removing objects from a sequence you're iterating over? Don't do that. Build a new list containing just the ones you want instead. – DSM Mar 29 '13 at 16:57
  • More info about what your are wanting to accomplish with this would help us provide a niffty way for you to do it. But yes you could resort to using the index. Just seems a shame. – cmd Mar 29 '13 at 16:59

2 Answers2

6

How do you control the index of a python for loop? (or can you? or should you?)

You can't / shouldn't - the loop control variable will be reassigned at the end of each iteration to the next element of whatever it is you are iterating over (so that i = i + 1 has no effect, since i will be reassigned to something different for the next iteration anyway). If you want to control the index like that, you should use a while-loop:

i = 0
while i < 10:
    print i
    i = i + 1

Although, Python's range function is more flexible than you might realize. For instance, to iterate in steps of 2 you can simply use something like

for i in range(0, 10, 2):
    print i
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • That's what I was thinking. Incrementing in twos (or something similar) is not really what I was looking for. So, while loop it is. Thanks! I'm going to look into this a little more and then accept the answer when I am confident in the solution. – Jordan Carroll Mar 29 '13 at 16:41
0

Check out the docs on range here, or from the docstr:

range([start,] stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.

To get range from 0-10, just do:

> for i in range(0, 11):
>     print i

> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10

By the way, it's pointless to do the i = i + 1, cause every iteration in the for loop will change i again. Whatever you set it to in the loop, will get overwritten every time the loop starts over.

Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
  • I realized that (i=i+1 is pointless) when I tested it. I was pretty disappointed. I think a while loop is the way to go though, incrementing in twos (or something similar) doesn't solve my issue. Thanks though! – Jordan Carroll Mar 29 '13 at 16:42