How might I refactor this loop into a Dict Comprehension? Should I?
for key,val in myDict.items():
if '[' in val:
myDict[key] = (ast.literal_eval(val))
For context, some of the values in the dictionary are lists, but formatted as strings; they come from an excel file. Anytime '[' is encountered in a cell, I want the dictionary value to be the literal cell value, not a string of it.
CodePudding user response:
Do you want something like this?
{key: ast.literal_eval(val) if '[' in val else val for key, val in myDict.items()}
In my opinion, for this case List Comprehension is less understandable than classic loop.