[Solved] Python: is the execution order of statements producing a tuple guaranteed? Multi-statement lambda

mutableVoid Asks: Python: is the execution order of statements producing a tuple guaranteed? Multi-statement lambda
I was wondering if code that is similar to the code sketched below would be legal in Python and safe to use:

Code:
# Example usage of a lambda which executes a statement and returns the changed operand
skip_first = lambda iterator: (next(iterator), iterator)[1]

Is it safe to use such a construct and expect the lambda’s return value to be an iterator which starts on the original iterator’s second element (if no exception is thrown)?

Or might it be legal for f.i. Cython to optimize away invoking next for some reason (i.e., because it discards the resulting value because the order of execution in a tuple is not defined and it can figure out that we discard the first value either way?)

  1. usage example:

Code:
f = iter([0, 1, 2, 3, 4])
print(list(skip_first(f)))

  1. Example on godbolt

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.