for elt in itertools.chain.from_iterable(node):
if elt is the last element:
do statement
How do I achieve this
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When the loop ends, the
eltvariable doesn’t go out of scope, and still holds the last value given to it by the loop. So you could just put the code at the end of the loop and operate on theeltvariable. It’s not terribly pretty, but Python’s scoping rules aren’t pretty either.The only problem with this (thanks, cvondrick) is that the loop might never execute, which would mean that
eltdoesn’t exist – we’d get aNameError. So the full way to do it would be roughly: