for elt in itertools.chain.from_iterable(node): if elt is the last element: do statement
How do I achieve this
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.
StopIteration