I am trying to update dictionaries in a list nodes with tuples in another list source with a conditional.
Tuple list:
source = [('144 IV 285', 16),
('144 IV 1', 11),
('141 IV 155', 7)]
Dictionary list:
nodes = [{'id': '144 IV 285','date': '2018-08-15','relevancy': 10, 'outDegree': 18},
{'id': '144 IV 240','date': '2016-08-15','relevancy': 4, 'outDegree': 10}]
Each item in 'nodes' should be extended by a new key (inDegree) value pair based on the 'source' list. My code:
for item in sources:
for item2 in nodes:
if item2["id"] == item[0]:
item2.update( {"inDegree": item[1]})
else:
item2.update( {"inDegree": 0})
Problem: How can I populate the key inDegree either by the value in the source list or 0, if there is no matching id for an item in 'nodes' in the 'source' list?
Aucun commentaire:
Enregistrer un commentaire