I’m trying to delete all elements from a Python list that are on an odd index of the list. In my case, the result from
len(elements) is 40, so I assume after deleting all odd indexes, the total length should be 20. I tried the following code:
indx = 0 for element in elements: if indx % 2 == 1: elements.remove(element) indx += 1
After this, the result of
len(elements) is 27, shouldn’t it be 20 since I deleted half of the elements?
It may be easier to just recreate the list with the indexes you want, e.g., by using a slice:
elements = elements[0::2]