Filtering two lists simultaneously
zip, filter and unzip again:
ids, other = zip(*((id, other) for id, other in zip(ids, other) if id not in del_ids))
The zip()
call pairs each id
with the corresponding other
element, the generator expression filters out any pair where the id
is listed in del_ids
, and the zip(*..)
then teases out the remaining pairs into separate lists again.
Demo:
>>> del_ids = [2, 4]
>>> ids = [3, 2, 4, 1]
>>> other = ['a', 'b', 'c', 'd']
>>> zip(*((id, other) for id, other in zip(ids, other) if id not in del_ids))
[(3, 1), ('a', 'd')]
zip, filter, unzip :
ids, other = zip(*filter(lambda (id,_): not id in del_ids, zip(ids, other)))