14

I'm trying to remove the odd-indexed elements from my list (where zero is considered even) but removing them this way won't work because it throws off the index values.

lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']


def remove_odd_elements(lst):
    i=0
    for element in lst:
        if i % 2 == 0:
            pass
        else:
            lst.remove(element)
        i = i + 1

How can I iterate over my list and cleanly remove those odd-indexed elements?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
avereux
  • 572
  • 1
  • 5
  • 15
  • Fun fact: to remove all even (positioned) elements you can do: `for x in lst: lst.remove(x)`. To remove all odds do: `iter_lst = iter(lst); next(iter_lst); for x in iter_lst: lst.remove(x)`. So, basically, removing the `if` solves the problem... I hope I don't have to say to never actually use this stuff in real code. – Bakuriu Mar 05 '15 at 17:59

3 Answers3

41

You can delete all odd items in one go using a slice:

del lst[1::2]

Demo:

>>> lst = ['712490959', '2', '623726061', '2', '552157404', '2', '1285252944', '2', '1130181076', '2', '552157404', '3', '545600725', '0']
>>> del lst[1::2]
>>> lst
['712490959', '623726061', '552157404', '1285252944', '1130181076', '552157404', '545600725']

You cannot delete elements from a list while you iterate over it, because the list iterator doesn't adjust as you delete items. See Loop "Forgets" to Remove Some Items what happens when you try.

An alternative would be to build a new list object to replace the old, using a list comprehension with enumerate() providing the indices:

lst = [v for i, v in enumerate(lst) if i % 2 == 0]

This keeps the even elements, rather than remove the odd elements.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

Since you want to eliminate odd items and keep the even ones , you can use a filter as follows :

>>>filtered_lst=list(filter(lambda x : x % 2 ==0 , lst))

this approach has the overhead of creating a new list.

jihed gasmi
  • 279
  • 4
  • 9
0

It's important to understand what is happening because modification during iteration is a common problem if it's not done right

Here is a simple list

0, 1, 2, 3, 4, 5

When idx = 1, you drop element 1:

0, 2, 3, 4, 5
   ^

Then you increment idx two more times before dropping another element:

0, 2, 3, 4, 5
         ^

So now you have

0, 2, 3, 5

Luckily (or unluckily), a list iterator allows you to modify the underlying data without breaking.

There are a couple of ways to fix this. The first one is to iterate backwards. Also, don't iterate over a collection when you modify it, keep the index external. You can still use a for loop though:

for idx in range(len(elements) - 1, -1, -1):
    if idx % 2:
        del elements[idx]

Don't forget to use del to drop elements by index instead of value.

If you're absolutely sure that the input has an even size, you can remove the conditional easily:

for idx in range(len(elements) - 1, -1, -2):
    del elements[idx]

For an arbitrary number of elements, it's only slightly more complicated to avoid the last potentially even element:

for idx in range(len(elements) // 2 * 2 - 1, -1, -2):
    del elements[idx]

In python, there are short-hand indices you can use to drop the elements directly. To do it in-place:

del elements[1::2]

Or the more commonly suggested option of making a copy:

elements = elements[::2]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264