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():
                    item[1]= ValueSplit

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:

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

>>> dict(keywords_list)
{'MEN': 'john,zack'}

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

| improve this answer | |
  • \$\begingroup\$ Is it possible to do something like the below to achieve this? [keyE [key.lower() for key in keywords_list] for keyE in extractedValue if keyE.lower() == key.lower()] \$\endgroup\$ – Dork Mar 18 '15 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.