Home > Back-end >  Compare list of tuples with list and return list of tuple that not matched
Compare list of tuples with list and return list of tuple that not matched

Time:09-25

I have list of tuple and list. Need to return that not matched from the list.

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

expected_result = [('CA', 'OS0003')]

CodePudding user response:

Try this:

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

CodePudding user response:

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)
  • Related