Reputation: 23
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
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
Reputation: 24049
Try this:
>>> [lt for lt in listTuple if not lt[1] in set(normalList)]
[('CA', 'OS0003')]
Upvotes: 1