A
A
annozzer2016-05-19 20:03:39
Python
annozzer, 2016-05-19 20:03:39

How to skip iteration?

Let's say I have a
for i in range(len(array)) loop:
how to make it so that after the 1st iteration it continues the loop, but already from 6?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
AtomKrieg, 2016-05-19
@AtomKrieg

arr = [1,2,3,4,5,6,7,8,9,10]
for i in range(len(arr)):
  if i in range(1,5): continue
  print(i)

E
Evgeniy _, 2016-05-19
@GeneD88

i = 0
while i < 20:
    i += 1
    print i
    if i == 1:
        i += 5

G
GavriKos, 2016-05-19
@GavriKos

So python or pluses? Those tags are weird.
On the plus side, make a for loop and increment the counter when you need to.
In python - probably it is also possible, in extreme cases - while i < len (array)

V
Vyacheslav, 2016-05-19
@Firik67

Walkthrough with step 2

for i in range(0, 10, 2):
    print i

In your case:
for i in range(0, len(array), 6):

A
Andy_U, 2016-05-20
@Andy_U

If you know where to "jump" after the first step before entering the loop, then you can do this:

for i in [0] + [j for j in range(6, len(array))]:
    print(array[i])

If the next index is determined after entering the loop, read about iterators and generators.

M
mikhail_404, 2016-05-20
@mikhail_404

It can be simpler by making the first iteration a direct call (if a function) and starting from the 6th.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question