nutship
nutship

Reputation: 4924

Filtering a list based on another list

I want to filter data based on items in drop.

data = [
    ['Basket', 'NBA ET', 'Kobe'],
    ['Basket', 'NCAA', 'Shaq'],
    ['Basket', 'ENG', 'Shaq'],
]
drop = ['NBA', 'NCAA']

And because I want the list with NBA ET to be left out too, it must be something beyond:

filtered = [d for d in data if d[1] not in drop]   # assume d[1] will hold

What I need is:

# pseudocode
filtered = [d for d in data if _ not in d[1] for _ in drop]

but I can never remember the syntax.

For the record, filtered should results in [['Basket', 'ENG', 'Shaq']]

Upvotes: 0

Views: 76

Answers (2)

Matt
Matt

Reputation: 17629

[row for row in data if not any(league in row[1].split() for league in drop)]
# [['Basket', 'ENG', 'Shaq']]

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121406

You can use any() and split the string on whitespace:

filtered = [d for d in data if not any(dropped in d[1].split() for dropped in drop)]

If you make drop a set, just test for an intersection:

drop = set(drop)

filtered = [d for d in data if not drop.intersection(d[1].split())]

The latter should be more performant the larger drop becomes.

Demo:

>>> data = [
...     ['Basket', 'NBA ET', 'Kobe'],
...     ['Basket', 'NCAA', 'Shaq'],
...     ['Basket', 'ENG', 'Shaq'],
... ]
>>> drop = ['NBA', 'NCAA']
>>> [d for d in data if not any(dropped in d[1].split() for dropped in drop)]
[['Basket', 'ENG', 'Shaq']]
>>> drop = set(drop)
>>> [d for d in data if not drop.intersection(d[1].split())]
[['Basket', 'ENG', 'Shaq']]

Upvotes: 2

Related Questions