Python: filter list of list with another list

Use a list comprehension:

result = [x for x in list_a if x[0] in list_b]

For improved performance convert list_b to a set first.

As @kevin noted in comments something like list(5,8)(unless it’s not a pseudo-code) is invalid and you’ll get an error.

list() accepts only one item and that item should be iterable/iterator

Leave a Comment