Possible Duplicate:
Python: Elegant and efficient ways to mask a list
I have two equal sized lists, something like:
a=["alpha","beta","kappa","gamma","lambda"]
b=[1,2,None,3,4,5]
What I would like to do is identify and delete the none element in list [b] and then remove the corresponding element in list [a]. Here, for example, I would like to delete none and “kappa”.
I am aware of:
filter(bool,b)
which would remove the None elements from [b], but, how do I go about deleting the corresponding entry in list[a]?
I tried zip, something like (the idea was to pack and unpack):
a=["a","b","c","d","e"]
b=[1,2,None,3,4]
c=zip(a,b)
d=filter(bool,c)
..but this does not work. [d] still has the none elements.
I would appreciate any pythonic way to achieve this.
This can be done neatly with
itertools.compress()and a list comprehension:This method means you only generate the selectors once (and
itertools.compress()should be nice and fast).