Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to FILTER Python list with multiple criteria?

Tags:

python

list

I have this list comprehension which returns elements in the list lst_fcflds if they are not in the list RROPFields:

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields]

and want a filter so that if OBJECTID or SHAPE are in lst_fclfds, they will also NOT be returned - like:

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields and not in ["OBJECTID","SHAPE"]]

like image 504
Robert Buckley Avatar asked Mar 15 '23 16:03

Robert Buckley


1 Answers

You're just missing one i

nfld_rrop = [i for i in lst_fcflds if i not in RROPFields and i not in ["OBJECTID","SHAPE"]]
                                                              ^

However for performance, I would add one step first to create a set so you can do faster membership lookups.

filters = set(RROPFields + ["OBJECTID", "SHAPE"])
nfld_rrop = [i for i in lst_fcflds if i not in filters]
like image 115
Cory Kramer Avatar answered Mar 18 '23 06:03

Cory Kramer