How to conditionally skip number of iteration steps in a for loop in python?

后端 未结 6 965

We have a list item_list,

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

We iterate over its items with a

6条回答
  •  遇见更好的自我
    2021-01-25 01:16

    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
    

提交回复
热议问题