Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Python list comprehension

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.

like image 950
PascalVKooten Avatar asked Apr 07 '26 06:04

PascalVKooten


2 Answers

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.

like image 142
DSM Avatar answered Apr 08 '26 20:04

DSM


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.

like image 21
axblount Avatar answered Apr 08 '26 21:04

axblount