I have a list of strings and each element of the list has several strings separated by colon. I am trying to convert each element into a dictionary. For example, one element in my list looks like this:
attributesList[0]
Out: 'Health Score: A, Happy Hour Specials: Yes, Vegan Options: Yes, Takes Reservations: Yes, Delivery: No, Take-out: Yes, Accepts Credit Cards: Yes, Good For: Brunch, Lunch, Dinner, Parking: Street, Bike Parking: Yes, Wheelchair Accessible: Yes, Good for Kids: No, Good for Groups: Yes, Ambience: Casual, Trendy, Classy, Noise Level: Average, Alcohol: Beer & Wine Only, Good For Happy Hour: Yes, Outdoor Seating: Yes, Wi-Fi: Free, Has TV: No, Waiter Service: Yes, Caters: No, Gender Neutral Restrooms: Yes'
Based on solutions in link 1 and link 2, I tried the following approaches:
attributesDict = dict(s.split(':') for s in attributesList)
attributesDict = dict(map(str.strip, s.split(':')) for s in attributesList)
attributesDict = dict(map(lambda s : s.split(':') for s in attributesList))
But I keep getting error messages shown below in each of the approaches:
ValueError: dictionary update sequence element #0 has length 24; 2 is required
ValueError: dictionary update sequence element #0 has length 24; 2 is required
TypeError: map() must have at least two arguments.
I looked at a solution here, but I am not clear how to fix the problem in my context. I am also a bit nervous about the presence of multiple items in my string after the colon as in the below case:
Good For: Brunch, Lunch, Dinner,
Can I capture the three items after the colon as a value in a dictionary? How I can I achieve what I am trying to?
Edit: adding desired output below
attributesDict[0]
Out: {'Health Score': 'A', 'Happy Hour Specials': 'Yes', 'Vegan Options': 'Yes', 'Takes Reservations': 'Yes', 'Delivery': 'No', 'Take-out': 'Yes', 'Accepts Credit Cards': 'Yes', 'Good For': 'Brunch, Lunch, Dinner', 'Parking': 'Street', 'Bike Parking': 'Yes', 'Wheelchair Accessible': 'Yes', 'Good for Kids': 'No', 'Good for Groups': 'Yes', 'Ambience': 'Casual, Trendy, Classy', 'Noise Level': 'Average', 'Alcohol': 'Beer & Wine Only', 'Good For Happy Hour': 'Yes', 'Outdoor Seating': 'Yes', 'Wi-Fi': 'Free', 'Has TV': 'No', 'Waiter Service': 'Yes', 'Caters': 'No', 'Gender Neutral Restrooms': 'Yes'}