Search code examples
pythonlistpython-2.7sublist

Python searching items in sulbist for a listA of items and returning index of not found items in listA


listA = ['HM', 'GL', 'WD', 'HM', 'WD', 'HM', 'WD']

sublist = [['HM','GL'], ['GL'], ['WD','HM'], ['WD','GL'], ['WD'], ['HM','WD'], ['HM']]

Both list and sublist are of equal lengths, the output should be mismatching index, i.e. [3,5] index number of list are not found in corresponding sublist.


Solution

  • I think correct answer should be [3, 6]:

    listA = ['HM', 'GL', 'WD', 'HM', 'WD', 'HM', 'WD']
    sublist = [['HM','GL'], ['GL'], ['WD','HM'], ['WD','GL'], ['WD'], ['HM','WD'], ['HM']]
    
    print([i for i, (a, b) in enumerate(zip(listA, sublist)) if not a in b])
    

    Prints:

    [3, 6]