Reputation: 29495
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?
Upvotes: 1
Views: 3087
Reputation: 25974
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)
Upvotes: 0
Reputation: 1630
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
Upvotes: 0
Reputation: 63777
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()
Upvotes: 1
Reputation: 59310
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
Upvotes: 1
Reputation: 114035
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
Upvotes: 2
Reputation: 184365
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.
Upvotes: 7