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?

4
  • You want to skip those particular items or any next three items in the list ? Commented Jan 16, 2014 at 18:00
  • You could use a counter and continue until it's 0, but i'm pretty sure the most pythonic way would be rethinking your loop entirely Commented Jan 16, 2014 at 18:01
  • @jramirez I want to skip next 3 items in the list Commented Jan 16, 2014 at 18:03
  • Then kindall's answer will do that for you. Commented Jan 16, 2014 at 18:04

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

7 Comments

What does next(list_iter) do?
@alwbtc it advances the iterator one position
You should put those next statements in a try ... except StopIteration
@alwbtc if you're new to iterators, you should read this
Instead of catching a StopIteration, since you're ignoring the value anyway, you can simply use the default arg, e.g. next(list_iter, None)
|
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

1 Comment

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
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

Comments

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()

Comments

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

Comments

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)

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.