I have a list that contains several strings and a dictionary with strings (that contain wildcards) as keys and integers as values.
For example like this:
list1 = ['i', 'like', 'tomatoes']
dict1 = {'tomato*':'3', 'shirt*':'7', 'snowboard*':'1'}
I would like to go through list1 and see if there is a key in dict1 that (with the wildcard) matches the string from list1 and get the respective value from dict1. So in this case 3
for 'tomato*'
.
Is there a way to iterate over list1
, see if one of the dict1 keys (with wildcards) matches with this particular string and return the value from dict1
?
I know I could iterate over dict1
and compare the keys with the elements in list1
this way. But in my case, the dict is very large and in addition, I have a lot of lists to go through. So it would take too much time to loop through the dictionary every time.
I thought about turning the keys into a list as well and get wildcard matches with a list comprehension and fnmatch()
, but the returned match wouldn't be able to find the value in the dict (because of the wildcard).