0

I'm new to python, so I apologise if this is straight forward. Other questions (here and here) have addressed lists of dicts, but I haven't been able to get this to work.

I have a list of dicts for each geographical area:

list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
                {'id': 'a', 'population': '20', 'area': '10'}]

I merely want to calculate the population density for each area, by dividing the 'population': 'value', by the 'area': 'value'. This calculation should create a new item.

The results should look like this:

results = [{'id': 'a', 'population': '20', 'area': '10', 'pop_density': 2},
           {'id': 'a', 'population': '30', 'area': '5', 'pop_density': 6}]
Thirst for Knowledge
  • 1,606
  • 2
  • 26
  • 43

4 Answers4

3

Alter the dictionaries

You can simply iterate over every dictionary, and associate 'pop_density' with the population density:

for v in list_of_dicts:
    v['pop_density'] = float(v['population'])/float(v['area'])

We need to use float(..) to convert a string '20' to the number 20. We can use int(..) if all values are ints. But perhaps it is safer to work with floats.

Copy the dictionaries

In case you want to create a copy of the list_of_dicts, you can use list comprehension:

[dict(v,pop_density=float(v['population'])/float(v['area'])) for v in list_of_dicts]

Generating:

>>> [dict(v,pop_density=float(v['population'])/float(v['area'])) for v in list_of_dicts]
[{'population': '20', 'area': '10', 'pop_density': 2.0, 'id': 'a'}, {'population': '20', 'area': '10', 'pop_density': 2.0, 'id': 'a'}]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

Changing original dictionaries

You can simply iterate over your list of dictionaries and the calculations. Make sure to round the result since you want an integer:

>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
                {'id': 'a', 'population': '20', 'area': '10'}]
>>> 
>>> for d in list_of_dicts:
    d['pop_density'] = int(d['population']) // int(d['area']) # round result by using //


>>> list_of_dicts
[{'pop_density': 2, 'population': '20', 'id': 'a', 'area': '10'}, {'pop_density': 2, 'population': '20', 'id': 'a', 'area': '10'}]
>>> 

Creating new dictionaries

Python 3

If you want a new list of dictionaries, you can use a list comprehension:

>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
                {'id': 'a', 'population': '20', 'area': '10'}]
>>> 
>>> [{'pop_densitiy': int(d['population']) // int(d['area']), **d} for d in list_of_dicts]
[{'area': '10', 'population': '20', 'id': 'a', 'pop_densitiy': 2}, {'area': '10', 'population': '20', 'id': 'a', 'pop_densitiy': 2}]
>>>

Python 2

Note the above uses the dictionary unpacking operator that is only available in python 3. If using Python 2, you'll need to use the dict constructor:

>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
                {'id': 'a', 'population': '20', 'area': '10'}]
>>> [dict(d, pop_densitiy=int(d['population']) // int(d['area'])) for d in list_of_dicts]
[{'pop_densitiy': 2, 'population': '20', 'id': 'a', 'area': '10'}, {'pop_densitiy': 2, 'population': '20', 'id': 'a', 'area': '10'}]
>>> 
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

Just iterate over the existing indices and add a new one to them:

results = []
for item in list_of_dicts:
    item = item.copy()  # since we want a new dict - mind you this is a shallow copy
    item["pop_density"] = int(item.get("population", 0)) / float(item.get("area", 1))
    results.append(item)
zwer
  • 24,943
  • 3
  • 48
  • 66
0
list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
                {'id': 'a', 'population': '30', 'area': '5'}]
[dict(j) for j in [ list(i.items()) + [ ('pop_density', int(i['population'])/int(i['area'])) ] for i in list_of_dicts ] ]

Output:

[{'area': '10', 'id': 'a', 'pop_density': 2.0, 'population': '20'},
 {'area': '5', 'id': 'a', 'pop_density': 6.0, 'population': '30'}]
Transhuman
  • 3,527
  • 1
  • 9
  • 15