-1

Set-up

I am working on a scraping project with Scrapy.

I have a list of strings,

l = ['john', 'jim', 'jacob', … ,'jonas']

and a dictionary containing lists,

d = {
 'names1': ['abel', 'jacob', 'poppy'],
 'names2': ['john', 'tom', 'donald']
}


Problem

I'd like to check for each name in l wether it is in contained in one of the lists in d. If so, I want the key of that list in d to be assigned to a new variable. E.g. jacob would get newvar = names1.

For now I have,

found = False
for key,value in d.items():
     for name in l:
          if name == value:
              newvar = key
              found = True
              break
     else:
         newvar = 'unknown'
     if found:
         break

However, this results in newvar = 'unknown' for every name in l.

I think I'm not breaking out of the outer for loop correctly. Other than that, is there perhaps a more elegant way to solve my problem?

LucSpan
  • 1,831
  • 6
  • 31
  • 66

1 Answers1

0

I think the issue you are having is you want to see if any string in l exists in a value in d. Your code now is assuming that the value is a string, not a list, and you are thus trying to make a comparison, which will always be false, therefore assigning "unkown" to every value. Try this code below:

l = ['john', 'jim', 'jacob', 'jonas']
d = {'names1': ['abel', 'jacob', 'poppy'], 'names2': ['john', 'tom','donald']}
found = False
for a, b in d.items():
   for i in l:
       if i in b:
           d[a] = "unkown"
           flag = True
           break

   if flag:
   break

print d

This will give you:

{'names2': 'unkown', 'names1': ['abel', 'jacob', 'poppy']}

Since you break out of the loop, only one key has an "unknown" value, but if you wanted to replace every value that contains a string in l with unkown, this will work as well:

final_dict = {a:"unkown" if i not in b else b for i in l for a, b in d.items()}

print final_dict
Ajax1234
  • 69,937
  • 8
  • 61
  • 102