“For” loop first iteration

前端 未结 13 2347
渐次进展
渐次进展 2020-12-04 15:17

I would like to inquire if there is an elegant pythonic way of executing some function on the first loop iteration. The only possibility I can think of is:

f         


        
13条回答
  •  攒了一身酷
    2020-12-04 15:42

    This works:

    for number, member in enumerate(something.get()):
        if not number:
            root.copy(member)
        else:
            somewhereElse.copy(member)
        foo(member)
    

    In most cases, though, I'd suggest just iterating over whatever[1:] and doing the root thing outside the loop; that's usually more readable. Depends on your use case, of course.

提交回复
热议问题