It may be work for itertools.tee. You "run" the sequence when checking, but after that you remain an untouched copy of the sequence:
from itertools import tee check, sequence = tee(sequence, 2) try: check.next(): except StopIteration:
(it is worth noting that tee does the โrightโ thing here: it only loads the first element of the sequence at the time check.next() is executed - and this first elument will remain available in the sequence The rest of the elements will be extracted only as part of the for loop Or simply: you cannot use len, you cannot check if the sequence is bool True, for the same reasons.
Therefore, your path is quite simple - another way would be to remove the name "item" before the "for" statement and check if it exists after the loop:
del item for item in sequence:
But your code should be used as more comprehensible than this.
jsbueno
source share