Search strings within a list from strings within a dictionary with the least complexity I have the following input:

newValue = [['men', 'John Doe,Chris perry'], ['women', 'Mary,Anne,Lita']] - list within list

keywords_list = [('MEN', 'john,zack')]    - tuple in a list (Dictionary on which .items() is used.)

Expected Output is:

newValue = [['men', 'John Doe'],['women','']]

What I want to do here is extract those names for Men which is common in both newValue and keywords_list.

Below is my working implementation that gets the desired result:

      allValues = []
      for item in newValue:
            for key in keywords_list:
                if str(item[0])[1:].upper() == key[0]:
                    ValueSplit = item[1].split(',')
                    keyWordSplit = key[1].split(',')
                    for EverySplit in ValueSplit:
                        for everyKey in keyWordSplit:
                            if everyKey.lower() in EverySplit.lower():
                                pass
                            else:
                                ValueSplit.remove(EverySplit)
                    item[1]= ValueSplit
      allValues.append(item)


This isn't an efficient way of solving the problem. If anyone could recommend an efficient way with lesser complexity to achieve the desired result, it would be really helpful.
both your datastructures can be cast into a dictionary directly:

&gt;&gt;&gt; dict(newValue)
{'men': 'John Doe,Chris perry', 'women': 'Mary,Anne,Lita'}

&gt;&gt;&gt; dict(keywords_list)
{'MEN': 'john,zack'}


from there you can just do a split based on , and compare in lowercase.