I am wondering if there is a standard library function in Python which will rearrange the elements of a list like below:
a = [1,2,3,4,5,6,7]
function(a)
print a
a = [1,7,2,6,3,5,4]
It should get one element from beginning of original list, then one from end, then second from beginning and so on. Then rearrange the list.
Regards,
You could build a fast, memory efficient generator using
itertoolswhich does what you want:You’ll find that
itertoolshas a great collection of building blocks for creating your own efficient iterators. A slightly more succinct solution:List comprehensions are another really concise way to build lists:
Finally here’s a short one-liner just for fun: