Python: Filter for Values where List A is Contained within List B

Question:

I have a list B:

pklFilenamesList = ['path202206_DATASET_STACK_.pkl', 'path202206_DATASET_OVER_.pkl',  'path202206_DATASET_FLOW_.pkl']

I have a list A:

listing = ['OVER','FLOW']

Attempted filter:

pickle= [x for x in pklFilenamesList if x[0] in listing]

which is returning a blank list []

Does anyone know how to filter for values from my List B contained in A?

Expected output:

pickle = ['path202206_DATASET_OVER_.pkl',  'path202206_DATASET_FLOW_.pkl']

any help is appreciated.

Asked By: Jonnyboi

||

Answers:

Need to check the value against every value on listing:

pickle = [x for x in pklFilenamesList if any(e in x for e in listing)]

print(pickle)

Result:

['pathx82206_DATASET_OVER_.pkl', 'pathx82206_DATASET_FLOW_.pkl']
Answered By: rdas
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.