import itertools def functional(a): return itertools.chain( itertools.chain.from_iterable(zip(a[1::2], a[::2])), [a[-1]] if len(a) % 2 else [] ) def gener(it): while True: try: a = next(it) except StopIteration: break try: b = next(it) except StopIteration: yield a break yield b yield a for upper in (10, 11): a = list(range(1, upper)) print(list(functional(a))) print(list(gener(iter(a))))
Standard input is empty
[2, 1, 4, 3, 6, 5, 8, 7, 9] [2, 1, 4, 3, 6, 5, 8, 7, 9] [2, 1, 4, 3, 6, 5, 8, 7, 10, 9] [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]