I have a bunch of lists that look like this one:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I want to swap elements as follows:
fi
Here a solution based in the modulo operator:
modulo
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] even = [] uneven = [] for i,item in enumerate(l): if i % 2 == 0: even.append(item) else: uneven.append(item) list(itertools.chain.from_iterable(zip(uneven, even)))