user308827
user308827

Reputation: 21961

Remove elements from python list based on filter

I have 3 lists in Python.

list_a = [10., 20., 30., 12.]
list_b = [30., 20., 60., 12.]
list_c = [10., 80., 90., 12.]

I want to drop those elements in list_b and list_c where values in list_a are <= 15. Therefore results become:

list_b = [20., 60.]
list_c = [80., 90.]

Is there a way to do this without for loops? (list comprehensions are ok)

Upvotes: 4

Views: 3649

Answers (6)

Mohammad Yusuf
Mohammad Yusuf

Reputation: 17064

If you are using numpy as you said you are, in comments, you can simply create a boolean index from a and mask elements in b and c:

import numpy as np

list_a = np.array([10., 20., 30., 12.])
list_b = np.array([30., 20., 60., 12.])
list_c = np.array([10., 80., 90., 12.])

list_b = list_b[list_a>15]
list_c = list_c[list_a>15]

print list_a
print list_b
print list_c

Output:

[ 10.  20.  30.  12.]
[ 20.  60.]
[ 80.  90.]

You can convert the lists_b and list_c to Python list type by .tolist() method.

Upvotes: 8

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You may write a one-liner to filter the list via using zip() as:

list_b, list_c = zip(*[(b, c) for a, b, c in zip(list_a, list_b, list_c) if a>15])

The final values hold by list_b and list_c will be:

>>> list_b
(20.0, 60.0)
>>> list_c
(80.0, 90.0)

Upvotes: 5

ettanany
ettanany

Reputation: 19806

You can zip() like below:

>>> list_a = [10., 20., 30., 12.]
>>> list_b = [30., 20., 60., 12.]
>>> list_c = [10., 80., 90., 12.]
>>>
>>> [j for i,j in zip(list_a, list_b) if i >= 15]
[20.0, 60.0]
>>>
>>> [j for i,j in zip(list_a, list_c) if i >= 15]
[80.0, 90.0]

Upvotes: 1

宏杰李
宏杰李

Reputation: 12158

list_a = [10., 20., 30., 12.]
list_b = [30., 20., 60., 12.]
list_c = [10., 80., 90., 12.]
drop = [i for i,v in enumerate(list_a) if v <=15]
b = [v for i,v in enumerate(list_b) if not i in drop]
c = [v for i,v in enumerate(list_c) if not i in drop]

Upvotes: 0

Sede
Sede

Reputation: 61225

You can use the little known itertools.compress class to do this. See Filter list using Boolean index arrays

>>> import itertools
>>> list_a = [10., 20., 30., 12.]
>>> list_b = [30., 20., 60., 12.]
>>> list_c = [10., 80., 90., 12.]
>>> bool_list = [item > 15 for item in list_a]
>>> bool_list
[False, True, True, False]
>>> list_b = list(itertools.compress(list_b, bool_list))
>>> list_b
[20.0, 60.0]
>>> list_c = list(itertools.compress(list_c, bool_list))
>>> list_c
[80.0, 90.0]

Upvotes: 7

Filip Malczak
Filip Malczak

Reputation: 3194

Can you use list comprehensions?

You could do it like this:

list_a = [10., 20., 30., 12.]
list_b = [30., 20., 60., 12.]
list_c = [10., 80., 90., 12.]
list_b = [ el for (i, el) in enumerate(list_b) if (list_a[i] > 15) ]
list_c = [ el for (i, el) in enumerate(list_c) if (list_a[i] > 15) ]

Snipper was written here, I haven't tested it, but you see the general idea.

I assumed that all lists are the same length. If list_a is shorter and you want to drop elements that are on the missing positions, you can do it like this:

list_b = [ el for (i, el) in enumerate(list_b) if (i<len(list_a) and list_a[i] > 15) ]

and if you want to keep them, just reverse the sign and boolean operator:

list_b = [ el for (i, el) in enumerate(list_b) if (i>=len(list_a) or list_a[i] > 15) ]

Upvotes: 2

Related Questions