Iterate again within the for loop

前端 未结 7 1015
走了就别回头了
走了就别回头了 2021-01-21 07:50

is there a way to iterate again within the for loop? For example:

for x in list:
  if(condition):
      #I\'d like to grab the next iteration of the lis         


        
7条回答
  •  情话喂你
    2021-01-21 08:27

    You can always bypass the for loop and just use the iterator explicitly:

    iter = list.__iter__()
    while True:
        x = iter.next()
        ...
        if (condition):
            x = iter.next()
        ...
    

    This will throw a StopIteration exception when it reaches the end of the list.

提交回复
热议问题