4

I am having an issue with last part of a code I'm creating. I am trying to, for example, make the list iterate to item 3 normally, but then check if the item is 3 and other condition (which doesn't matter right now), then change the index to iterate from example 10.

I made a lot of attempts but it doesn't seem to work.

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(0,len(li)):
    print(i)
    if i == 3: #along with other condition
        def g(li):
            global i
            i = li[9]
        g()
        print(i)
martineau
  • 119,623
  • 25
  • 170
  • 301
DC55
  • 47
  • 1
  • 1
  • 3
  • 4
    Why use a nested function at all? Also, a Python `for` loop is different than a Java/C `for` loop; the value of `i` will be replaced, not modified, in the next iteration. Maybe use a `while` loop instead? – tobias_k Sep 13 '18 at 15:01

8 Answers8

3

you can use the continue statement:

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(len(li)):
    if 3 <= i < 10: #along with other condition
        continue
    print(i)

output when printing i:

0,1,2,10,11,12,13,14,15

output when printing li[i]:

1,2,3,11,12,13,14,15,16

the continue statement brings you at the beginning of the loop, ignoring all the following conditions.
you may want to have a look to the loop control statements

Gsk
  • 2,929
  • 5
  • 22
  • 29
1

A for loop isn't necessary. If you're happy using a 3rd party library, you can use NumPy:

import numpy as np

A = np.array(li)

res = A[np.r_[:4, 9:len(A)]]

# array([ 1,  2,  3, 10, 11, 12, 13, 14, 15, 16])

Or with regular Python you can use slice objects:

from operator import itemgetter
from itertools import chain

slices = (slice(0, 4), slice(9, None))

res = list(chain.from_iterable(itemgetter(*slices)(li)))

# [1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16]
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You are setting the value of i to some value inside your for loop. At the beginning of each iteration of the for loop, python updates the value of i to be the next value in the iterable that it is iterating over. Therefor, your value is lost and not used.

One solution would be to use another variable (skip_until) like so:

lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
skip_until = -1
for i in range(len(lst)):
    if i == 3:
        skip_until = 9
    if skip_until >= i:
        continue      
    print((i, lst[i]))

output:

(0, 1) (1, 2) (2, 3) (10, 11) (11, 12) (12, 13) (13, 14) (14, 15) (15, 16)

c.w
  • 13
  • 2
0
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
condition3_reached = False
condition10_reached = False
for i in li:
    print(i)
    if conditition3_reached and not condition10_reached and i != 10:
        continue
    if condition3_reached and i == 10:
        condition10_reached = True
    if i == 3 and (#along with other condition):
        condition3_reached = True
        print(i)
    else:
        do_some_new_thing_for_10_onwards()

this is a simple way of achieving what you want. My worry is that it is not scalable

Landar
  • 278
  • 1
  • 10
0

Should use continue like so:

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
for i in range(0,len(li)):
    if i in range(3, li[9]):
        continue
    print(i)
Adelina
  • 10,915
  • 1
  • 38
  • 46
0

The most simple and readable way to accomplish this is through the use of a while as opposed to the for loop as depicted below.

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
i = 0
while i < len(li):
    print(i)
    if i == 3:
        i = 10
        print(i)
    i += 1 # increment i at the end of the loop
Philip DiSarro
  • 1,007
  • 6
  • 9
0

i try to understand what you said and i wrote this code

li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
def k(pos = 0):
    for i in li[pos:]:
        print(i)
        if li.index(i) == 3: #along with other condition
            return k(pos = 9)
k()
KC.
  • 2,981
  • 2
  • 12
  • 22
0

What you want to do is not really possible with a Python for loop, which is less than a Java/C style for (initializer; step; condition) loop, but more like a foreach (iterable) loop, where the iterable just happens to be a range in your case.

Thus, whenever you do i = ... in your loop (i being the variable from the for loop), i will be overwritten (not modified) with a new value in the next iteration of the loop.

Instead, you can use a slightly longer while loop:

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        def g(li):
            global i
            i = li[9]
        g(li)
    else:
        i += 1

Also note that the nested function g apparantly does not serve any purpose and can be removed, although the case may be different in your actual code.

i = 0
while i < len(li):
    print(i)
    if i == 3: #along with other condition
        i = li[9]
    else:
        i += 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179