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

后端 未结 6 902

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

    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)
    

提交回复
热议问题