0
List1 = [['A0.01', 'GENERIC NOTES'],['A0.02', 'ANOTHER GENERIC NOTE'],['A0.03', 'YET ANOTHER GENERIC NOTE']]
List2 = ['A0.01','A0.03']

and I want to find the intersection of the two lists and return a list of all items if there is a match. Below is what I have tried:

result = [[j for j in view if j in List2] for view in List1]

This returns me the matching values in List1

[['A0.01'],[],['A0.03']]

but I want the rest of the values within that matching list item as well. Below is what I intend the result to be:

['A0.01', 'GENERIC NOTES'],['A0.03', 'YET ANOTHER GENERIC NOTE']

How can I achieve this. I appreciate any help!

Simon Palmer
  • 384
  • 1
  • 2
  • 13
  • Does this answer your question? [How to check if one of the following items is in a list?](https://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list) – mkrieger1 Aug 15 '20 at 19:37
  • 2
    `[x for x in List1 if any(y in x for y in List2)]` – mkrieger1 Aug 15 '20 at 19:38

1 Answers1

0

I'm sure there are many ways to do this.

Here is one

>>> matches = set(item[0] for item in List1).intersection(List2)
>>> matches
{'A0.03', 'A0.01'}
>>> [item for item in List1 if item[0] in matches]
[['A0.01', 'GENERIC NOTES'], ['A0.03', 'YET ANOTHER GENERIC NOTE']]
Bill
  • 10,323
  • 10
  • 62
  • 85