Ashu
Ashu

Reputation: 23

Compare list of tuples with list and return list of tuple that not matched

I have list of tuples and a second list. I need to return a list of tuples for which the second item in the tuple does not appear in the second list.

listTuple =  [('IN', 'OS19014'), ('CA', 'OS0001'), ('GB', 'OS0002'), ('CA', 'OS0003')]
normalList = ['OS19014', 'OS0001', 'OS0002']

Expected_result:

[('CA', 'OS0003')]

Upvotes: 1

Views: 51

Answers (2)

salma baian
salma baian

Reputation: 7

this code work for me:

listTuple = [('IN', 'OS19014'), ('CA', 'OS0001'), ('GB', 'OS0002'), ('CA', 'OS0003')] 
normalList = ['OS19014', 'OS0001', 'OS0002']
for i in listTuple:
    if i[1] not in normalList:
        print(i)

Upvotes: 0

I'mahdi
I'mahdi

Reputation: 24049

Try this:

>>> [lt for lt in listTuple if not lt[1] in set(normalList)]
[('CA', 'OS0003')]

Upvotes: 1

Related Questions