Python How to I check if last element has been reached in iterator tool chain?

后端 未结 4 447
臣服心动
臣服心动 2021-01-31 20:29
for elt in itertools.chain.from_iterable(node):

if elt is the last element:
  do statement

How do I achieve this

4条回答
  •  面向向阳花
    2021-01-31 20:58

    I do something like this:

    rng = len(mlist)
    for i in range(rng):
        foo = mlist[i]
        foo.do_something_for_every_item_regardless()
        if i == rng - 1: #since we go from 0 to rng-1
            foo.last_item_only_operation()
    

提交回复
热议问题