Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Delete all list indices meeting a certain condition

to get right down to it, I'm trying to iterate through a list of coordinate pairs in python and delete all cases where one of the coordinates is negative. For example:

in the array:

map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]

I want to remove all the pairs in which either coordinate is < 0, leaving:

map = [[2, 3], [7, 1]]

My problem is that python lists cannot have any gaps, so if I loop like this:

i = 0
for pair in map:
        for coord in pair:
            if coord < 0:
                del map[i]
    i += 1

All the indices shift when the element is deleted, messing up the iteration and causing all sorts of problems. I've tried storing the indices of the bad elements in another list and then looping through and deleting those elements, but I have the same problem: once one is gone, the whole list shifts and indices are no longer accurate.

Is there something I'm missing?

Thanks.

like image 333
Will Avatar asked Aug 23 '11 15:08

Will


People also ask

How do I remove a list of lists indices in Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do you remove an element from a list based on a condition?

To remove elements from ArrayList based on a condition or predicate or filter, use removeIf() method. You can call removeIf() method on the ArrayList, with the predicate (filter) passed as argument. All the elements that satisfy the filter (predicate) will be removed from the ArrayList.

How do I remove multiple indexes from a list?

Remove Multiple elements from list by index range using del. Suppose we want to remove multiple elements from a list by index range, then we can use del keyword i.e. It will delete the elements in list from index1 to index2 – 1.


1 Answers

If the list is not large, then the easiest way is to create a new list:

In [7]: old_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]

In [8]: new_map=[[x,y] for x,y in a_map if not (x<0 or y<0)]

In [9]: new_map
Out[9]: [[2, 3], [7, 1]]

You can follow this up with old_map = new_map if you want to discard the other pairs.

If the list is so large creating a new list of comparable size is a problem, then you can delete elements from a list in-place -- the trick is to delete them from the tail-end first:

the_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
for i in range(len(the_map)-1,-1,-1):
    pair=the_map[i]
    for coord in pair:
        if coord < 0:
            del the_map[i]

print(the_map)

yields

[[2, 3], [7, 1]]

PS. map is such a useful built-in Python function. It is best not to name a variable map since this overrides the built-in.

like image 163
unutbu Avatar answered Sep 22 '22 03:09

unutbu