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

后端 未结 4 439
臣服心动
臣服心动 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:49

    You can't per se. You'd need to store the current item, advance the iterator, and catch the StopIteration exception. And then you'd need to somehow signal that you have the last item.

提交回复
热议问题