List of strings:
['Georgie Porgie', 87, '$$$', ['Canadian', 'Pub Food'], 'Queen St. Cafe', 82, '$', ['Malaysian', 'Thai'], 'Dumplings R Us', 71, '$', 'Chinese', 'Mexican Grill', 85, '$$', 'Mexican', 'Deep Fried Everything', 52, '$', 'Pub Food']
I am trying to create a cuisine_to_names dictionary out of the list above. The cuisines are at index 3 and are sometimes a mini-list of their own. The restaurant names are at index 0. They repeat every fourth index. cuisines - 3::4, names - 0::4.
The issue that I have is extracting elements out of index 3::4 and making them keys. I think the trouble comes because sometimes they are a mini list of strings and sometimes they are just a string. This made using defaultdict difficult for me, but I'm new to learning that function. I saw some other answers include things like setdefault, but I have no idea how to use that for this specific case. Any guidance would be appreciated!
I would like this output:
Cuisine to list of restaurant names:
# dict of {str, list of str}
{'Canadian': ['Georgie Porgie'],
'Pub Food': ['Georgie Porgie', 'Deep Fried Everything'],
'Malaysian': ['Queen St. Cafe'],
'Thai': ['Queen St. Cafe'],
'Chinese': ['Dumplings R Us'],
'Mexican': ['Mexican Grill']}
I tried this and got TypeError: unhashable type: 'list':
from collections import defaultdict
cuisine_to_name = defaultdict(list)
for cuisine, name in zip(contents_list_2[3::4], contents_list_2[::4]):
cuisine_to_name[cuisine].append(name)
print(cuisine_to_name)