Is there any way to convert this dictionary:
d = {'Ano; Dia; Mes' : '1995; 2; 5'}
for this format:
d = {'Ano': '1995'; 'Dia' : '2'; 'Mes': '5'}
Thank you
Is there any way to convert this dictionary:
d = {'Ano; Dia; Mes' : '1995; 2; 5'}
for this format:
d = {'Ano': '1995'; 'Dia' : '2'; 'Mes': '5'}
Thank you
First I take the keys ( d.keys()
) and transform them into a list, then I get the first element (of value 0
, 'Ano; Dia; Mes'
) and separate them from "; "
getting a list: ['Ano', 'Dia', 'Mes']
.
Then I do the same with the values ( d.values()
).
Then I put those values into the dictionary using for
.
It was kind of confusing, but you can understand ...
Looking like this:
d = {'Ano; Dia; Mes' : '1995; 2; 5'}
d2 = {}
chaves = list(d.keys())
valores = list(d.values())
chaves = chaves[0].split('; ')
valores = valores[0].split('; ')
for i in range(len(chaves)):
d2[chaves[i]] = valores[i]
print(d2)
Output :
>>> {'Ano': '1995', 'Mes': '5', 'Dia': '2'}