I was wondering if code that is similar to the code sketched below would be legal in Python and safe to use:
# 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?)
- usage example:
f = iter([0, 1, 2, 3, 4])
print(list(skip_first(f)))