I am relatively new to python and I am still trying to learn the basics of the language. I stumbled upon a question which asks you to rearrange the list by modifying the original. What you are supposed to do is move all the even index values to the front (in reverse order) followed by the odd index values.
Initial:
0 1 2 3 4 5 6
[3, 5, 2, 8, 7, 9, 0]
Rearranged:
6 4 2 0, 1, 3, 5
[0, 7, 2, 3, 5, 8, 9]
This is how I approached the question:
even = []
odd = []
for i in range(len(l)):
if i % 2 == 0:
even.append(i)
else:
odd.append(i)
even = even[::-1]
index = even + odd
l2 = []
for i in index:
l2.append(l[i])
for i in range(len(l)):
l[i] = l2[i]
Although it works, and does what is being asked, I was wondering if there is a more efficient way to get the result. I feel that creating three empty lists just to rearrange one list is not the best approach.
Any input would be greatly appreciated.