I have a list of dictionaries, like this:
ls_dict = [{'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, {'A': 65, 'B': 66, 'C': 67}, {'a': 97, 'b': 98, 'c': 99}]
Which produces this:
[{'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, {'A': 65, 'B': 66, 'C': 67}, {'a': 97, 'b': 98, 'c': 99}]
What I want is a dictionary of dictionaries where each dictionary is indexed like the following:
{"0": {'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, "1": {'A': 65, 'B': 66, 'C': 67}, 2: {'a': 97, 'b': 98, 'c': 99}}
The additional problem is that I need to convert the former to the latter, rather than adapting my sample creation code, as I just spent 15 hours downloading the data into the current (wrong) format.
Any suggestions? Thank you.
Advertisement
Answer
You can do with enumerate
, so:
In [75]: {str(index): lst for index, lst in enumerate(ls_dict)} Out[75]: {'0': {'py': 'Python', 'mat': 'MATLAB', 'cs': 'Csharp'}, '1': {'A': 65, 'B': 66, 'C': 67}, '2': {'a': 97, 'b': 98, 'c': 99}}