Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
enumerate iterable solution in Clear category for Swap Nodes by Phil15
def swap_nodes(iterable): # works with any iterable, not just lists.
i = 1 # in case of an empty iterable, i doesn't exit without that.
for i, elem in enumerate(iterable):
if i%2:
yield elem
yield previous
else:
previous = elem
if not i%2:
yield previous
Jan. 22, 2019
Comments: