1

We have a list item_list,

item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]

We iterate over its items with a for loop, if item is "XYZ", skip items "c", "d", "e" and continue with "f":

for item in item_list:
    if item == "XYZ":
       do_something()
       skip_3_items() ----> skip items "c", "d", "e"
    else:
        do_something_else()

What could be the most pythonic way to achieve this?

alwbtc
  • 28,057
  • 62
  • 134
  • 188

6 Answers6

7
list_iter = iter(item_list)

for item in list_iter:
    if item == "XYZ":
        do_something()
        for _ in range(3):   # skip next 3 items
            next(list_iter, None)

# etc.

Basically, rather than iterating over the list directly, you create an abstraction for it called an iterator and iterate over that. You can tell the iterator to advance to the next item by calling next(...) which we do three times to skip the next three items. The next time through the loop, it picks up at the next item after that.

kindall
  • 178,883
  • 35
  • 278
  • 309
2

Since nobody has mentioned a while loop, I will:

item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]
i = 0
while i < len(item_list):
    item = item_list[i]
    if item == "XYZ":
        do_something()
        i += 3
    else:
        do_something_else()
    i += 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • I believe no one has mentioned because it is usually very "non-pythonic" to use while. The whole point of using iterators is to get away from counting integers and stuff. Your code works, of course – lsoranco Mar 31 '17 at 16:57
1

Use an iterator:

$ cat t.py 
item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]

it = iter(item_list)
for item in it:
    if item == "XYZ":
       print item
       for _ in range(3):
           next(it, None)
    else:
        print item

This gives:

$ python t.py 
a
b
XYZ
f
g
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
1

I would split the processing it two parts for sake of readability

>>> def foo(item_list,key = "XYZ", skip = 3):
    from itertools import takewhile, islice
    def do_something():
        return "do_something()"
    def do_something_else():
        return "do_something_else()"
    it = iter(item_list)
    for items in takewhile(lambda e: e != key, it):
        print items, do_something_else()
    print do_something()
    it = islice(it,skip, None)
    for items in it:
        print items, do_something_else()


>>> foo(item_list)
a do_something_else()
b do_something_else()
do_something()
f do_something_else()
g do_something_else()
Abhijit
  • 62,056
  • 18
  • 131
  • 204
0

I'd store a counter that handles skipping items.

def skipper(item_list):
    skip_count = 0
    for item in item_list:
        if item == "XYZ":
            skip_count = 3
        else:
            if skip_count:
                skip_count -= 1
            else:
                # do_something()
                print item,

Example:

In [23]: item_list
Out[23]: ['a', 'b', 'XYZ', 'c', 'd', 'e', 'f', 'g']

In [24]: skipper(item_list)
a b f g
Chris Arena
  • 1,602
  • 15
  • 17
0

I'd probably write it out like this, personally:

xyz_i = item_list.index('XYZ')

do_something('XYZ') #or do_something(item_list[xyz_i]) but.. just save yourself the list lookup

for x in item_list[xyz_i+4:]:
    do_something_else(x)
roippi
  • 25,533
  • 4
  • 48
  • 73