Skip to content
Advertisement

Extract elements of a list of tuples based on elements in another list

I have a list of lists

JavaScript

and a list of tuples

JavaScript

I want to extract those tuples from list_tuples whose first element is listed in list_x. The desired output is

JavaScript

I tried it by using itemgetter (from operator import itemgetter). It works well with dicts but I could not apply it to this problem since it cannot work with a list as a list index (at least that is what the error said).

JavaScript

Any solution would be great (a solution with itemgetter would be even greater). Thank you.

Advertisement

Answer

Try:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement