Identify which iteration you are on in a loop in python

前端 未结 5 579
走了就别回头了
走了就别回头了 2020-12-11 15:33

Basically I would like to be able to tell when I\'m on the Nth item in a loop iteration. Any thoughts?

d = {1:2, 3:4, 5:6, 7:8, 9:0}

for x in d:
    if last         


        
5条回答
  •  眼角桃花
    2020-12-11 16:23

    d = {1:2, 3:4, 5:6, 7:8, 9:0}
    
    for i,x in enumerate(d):
        print "last item :"+repr(x) if i+1==len(d) else x
    

    But the last item of an unordered dictionary doesn't mean anything

提交回复
热议问题