How do turn this:
list1 = [{'A': '1', 'B': '2', 'C': '3'}, {'D': '4', 'E': '5', 'F': '6'}, {'G': '7', 'H': '8', 'I': '9'}]
Into this:
dict1 = {'A': '1', 'B': '2', 'C': '3'}
dict2 = {'D': '4', 'E': '5', 'F': '6'}
dict3 = {'G': '7', 'H': '8', 'I': '9'}
CodePudding user response:
You could try this. But I would not suggest this way of coding. Better define the variables you want explicitly instead of forming them on the fly.
list1 = [{'A': '1', 'B': '2', 'C': '3'}, {'D': '4', 'E': '5', 'F': '6'}, {'G': '7', 'H': '8', 'I': '9'}]
for i, j in enumerate(list1):
exec(f'dict{i 1}={str(j)}')
print(dict1)
print(dict2)
print(dict3)
This way you can create as many variables as you want from the list
CodePudding user response:
This thing that you are trying to do isn't a very good practise, in-fact one of the worse. Just use it where ever you need it by list1[0]
, list1[1]
etc.