2

I have a nested list of the following structure:

nested = [["a","b"], ["c", "d"]]

I now want to stick to this structure, but remove elements if they belong to another list. Imagine this list to be stoplist = ["a","z"]

So the result would be:

[["b"], ["c", "d"]] 

I am hoping I am missing a simple thing here, but I just can't seem to get the list comprehension in this case:

[letter for letter in List if letter not in stoplist for List in nested],

it runs, but it gives back this result: ['c', 'c', 'd', 'd']

What is going on, and how to solve this?

Note: I understand this can be done with append, but I would prefer to avoid this, as I will be working with big files.

3
  • You probably want to convert stoplist to a set. Commented Oct 17, 2013 at 21:10
  • Is the order of the results important? In particular, is the order of the sublists important? Commented Oct 17, 2013 at 21:11
  • @kojiro Yes, it is fundamental. Commented Oct 17, 2013 at 21:34

3 Answers 3

5

Maybe something like

>>> nested = [["a","b"], ["c", "d"]]
>>> stoplist = ["a", "z"]
>>> [[letter for letter in sublist if letter not in stoplist] for sublist in nested]
[['b'], ['c', 'd']]

Although if what's in stoplist is hashable, it might be faster to make it a set (although it's hard to guess for really small collections -- timeit and find out if it matters).

>>> stopset = set(stoplist)
>>> [[letter for letter in sublist if letter not in stopset] for sublist in nested]
[['b'], ['c', 'd']]

Your current listcomp can be unpacked into

newlist = []
for letter in List:
    if letter not in stoplist:
        for List in nested:
            newlist.append(letter)

which (and this puzzled me for a few minutes) shouldn't really work at all. It must be picking up List from an earlier run.

Note that the order you write the nesting in a list comprehension is the same way you'd write the equivalent nested for loops.

Sign up to request clarification or add additional context in comments.

Comments

2

Try

[ [letter for letter in List if letter not in stoplist] for List in nested]

Note that this will only work if nested is nested one level deep.

Comments

2

Here's another one, make stoplist a set and then just use set difference operation:

>>> stoplist = {'a', 'z'}
>>> [list(set(l)-stoplist) for l in nested]
[['b'], ['c', 'd']]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.