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?