Dictionaries for Dictionaries List

0

I have a dictionary like the following:

{'Eva': [4], 'Ana': [53], 'Ada': [12]}

And I want to turn it into a list of dictionaries in this type:

[{'Nome do Dono': 'Eva', 'Media de idades' : '4'},{'Nome do Dono':'Ana':'Media das Idades':'53'},{'Nome do Dono': 'Ada', 'Media de idades' : '12'}]

They can help on getting the list of dictionaries as intended

    
asked by anonymous 25.04.2016 / 16:13

2 answers

0

To get the list of dictionaries do so:

dict_1 = {'Eva': [4], 'Ana': [53], 'Ada': [12]}  
lista_dict = [{'Media de Idade':idade.pop(), 'Nome do Dono':nome} for nome, idade in dict_1.items()]
    
26.04.2016 / 00:17
0

Assuming you got the value 4,666 from the 28 by 6 split, you can use the ceil to round up:

import math

age_avg = 28 / 6.0 # 4.666666666666667
print age_avg # imprime 4.666666666666667
print math.ceil(age_avg) # imprime 5
    
26.04.2016 / 22:59