1

There are already several good answers to this question [1]. Here, I was trying to increment the 'i' in outer for-loop from inner for-loop and did not work as expected. Therefore posting this question and answer. Hoping, it may help someone in future.

x = [0,1,2,3,4,5,6,7,8,9,10]
for i in x:  
  print(x[i])  
  for j in x:
    i += 1

Output:

i:0; x[i]:0
i:1; x[i]:1
i:2; x[i]:2
i:3; x[i]:3
i:4; x[i]:4
i:5; x[i]:5
1

1 Answer 1

0

Answer:

  1. Increment the 'start' in range(start, stop, step)
  2. Then, check for iterator >= start

Code:

x = [0,1,2,3,4,5,]
x_start=0

for i in range(x_start, len(x)):

  if(i>=x_start):    
    print('i:{}; x[i]:{}'.format(i, x[i]))

    for j in range(2):
      x_start += 1

Output:

i:0; x[i]:0
i:2; x[i]:2
i:4; x[i]:4
Sign up to request clarification or add additional context in comments.

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.