I have a list
x=[0,1,1,2,3,5,8,3,1]
I want to remove list items at odd index only
i.e. x[1],x[3],x[5],...
etc
should be removed.
My resulting list should be
x=[0,1,3,8,1]
def odd_indexed_list(x):
...
x = [0, 1, 1, 2, 3, 5, 8, 3, 1]
# ^ ^ ^ ^ ^
odd_indexed_list(x)
print(x)
# >> [0, 1, 3, 8, 1]
I want to loop till only one item in the list is remaining.
Here's my version of odd_indexed_list()
def odd_indexed_list(x):
n=len(x)
while(n>1):
for j in range(1,n+1,2):
del x[i]
The error is - IndexError: list assignment index out of range