Reputation: 451
I am trying to learn / teach myself Python3 and I'm working on reformatting scraped web data.
Input is a list of dictionaries:
[{'name' : 'Anna', 'gender': "f", 'ID': 512128, 'age' : 28},
{'name' : 'Barney', 'gender': "m", 'ID': 012428, 'age' : 29},
{'name' : 'Cesar', 'gender': "m", 'ID': 032536, 'age' : 57}]
I would like to achieve a list of dictionaries with every dictionary having an additional field names 'colleagues', which contains the information of the other dictionaries in the list.
Desired output:
[{'name' : 'Anna', 'gender': "f", 'ID': 512128, 'age' : 28, 'colleagues' : [{'name' : 'Barney', 'gender': "m", 'ID': 012428, 'age' : 29}, {'name' : 'Cesar', 'gender': "m", 'ID': 032536, 'age' : 57}]},
{'name' : 'Barney', 'gender': "m", 'ID': 012428, 'age' : 29, 'colleagues' : [{'name' : 'Anna', 'gender': "f", 'ID': 512128, 'age' : 28}, {'name' : 'Cesar', 'gender': "m", 'ID': 032536, 'age' : 57}]},
{'name' : 'Cesar', 'gender': "m", 'ID': 032536, 'age' : 57, 'colleagues' : [{'name' : 'Anna', 'gender': "f", 'ID': 512128, 'age' : 28}, {'name' : 'Barney', 'gender': "m", 'ID': 012428, 'age' : 29}]}]
As you can tell, I am new to python but understand simple comprehension and for loops.. etc. I spent the last night with all possible attempts and very nested loop structures - which were causing infinite loops.
I hope one of the experts can help me how to approach that task, any help is greatly appreciated.. Thanks!
Upvotes: 0
Views: 48
Reputation: 198324
output = [dict([*p.items(),
['colleagues', [q for q in input if p != q]]
]) for p in input]
You might be able to speed it up by using sets and inputset - set(p)
.
The whole dict([*p.items(), [k, v]])
is kind of a hack to make a new dict with an extra key-value pair without having to use statements. It is pretty much equivalent to dict_with_new_pair(p, k, v)
given:
def dict_with_new_pair(d, k, v):
from copy import copy
c = copy(d)
c[k] = v
return c
EDIT: As you said, both p
and q
are elements of input
. The expression parses like this: For every person p
in input
, enrich it with key colleagues
, whose value will be the all persons q
in input
that are not p
(and collect the list of all such enriched p
into a new list output
).
Upvotes: 1