We have a list item_list,
item_list
item_list = [\"a\", \"b\", \"XYZ\", \"c\", \"d\", \"e\", \"f\", \"g\"]
We iterate over its items with a
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