Python: filter list of list with another list -


i'm trying filter list, want extract list (is list of lists), elements matches key index 0, list b has serie of values

like this

list_a = list(   list(1, ...),   list(5, ...),   list(8, ...),   list(14, ...) )  list_b = list(5, 8)  return filter(lambda list_a: list_a[0] in list_b, list_a) 

should return:

list(     list(5, ...),     list(8, ...) ) 

how can this? thanks!

use list comprehension:

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

for improved performance convert list_b set first.

as @kevin noted in comments list(5,8)(unless it's not pseudo-code) invalid , you'll error.

list() accepts 1 item , item should iterable/iterator


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -