Dictionary for list of dictionaries

Asked

Viewed 677 times

0

I have a dictionary like the following:

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

And I intend to turn it into a list of dictionaries for this guy :

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

Can help how to get the list of dictionaries as I intend

2 answers

1


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()]

0

Assuming you have gotten the value 4.666 from the division of 28 by 6, you can use the function Ceil to effect rounding:

import math

age_avg = 28 / 6.0 # 4.666666666666667
print age_avg # imprime 4.666666666666667
print math.ceil(age_avg) # imprime 5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.