I'm trying to get rid of duplicated dictionaries with the same keys (by merging them), but also need to keep track of the values they contain. I have 2 lists:
person_list_1 = [
{"name": "John",
"cars": [
{"make": "Audi", "model_year": 2001},
{"make": "BMW", "model_year": 2009}
]
},
{"name": "Mary",
"cars": [
{"make": "Mazda", "model_year": 2003}
]
},
]
person_list_2 = [
{"name": "John",
"cars": [
{"make": "Audi", "model_year": 2017},
{"make": "Volkswagen", "model_year": 2008}
]
},
{"name": "Ben",
"cars": [
{"make": "Ford", "model_year": 1984}
]
}
]
As you can see, two of them have the same name key, but the values are kind of different.
I would like it to look like this eventually:
updated_person_list = [
{"name": "John",
"cars": [
{"make": "Audi", "model_year": 2017},
{"make": "BMW", "model_year": 2009},
{"make": "Volkswagen", "model_year": 2008},
]
},
{"name": "Mary",
"cars": [
{"make": "Mazda", "model_year": 2003},
]
},
{"name": "Ben",
"cars": [
{"make": "Ford", "model_year": 1984}
]
},
]
I.e., find matching dictionary keys between both lists and account for them accordingly - if such a car already exists in the "cars" list but with a different production year, update the nested dictionary.
Tried out many different solutions including collections.defaultdict, itertools.chain, etc, but none of those attempts was successful.
Can anyone please help with this?
CodePudding user response:
You should iterate over dictionaries and accumulate the items inside temporary dictionary with names as a keys and values as dictionary of cars from make to dict of cars, and finally we convert this dictionary to our desired dictionary:
def iterate(acc, l):
for p in l:
name = p["name"]
if not name in acc:
acc[name] = {}
for c in p["cars"]:
acc[name][c["make"]] = c
def merge(l1, l2):
acc = {}
iterate(acc, l1)
iterate(acc, l2)
return acc
def convert(acc):
resp = []
for k, v in acc.items():
resp.append({"name": k, "cars": list(v.values())})
return resp
We can use this functions as:
print(convert(merge(person_list_1, person_list_2)))
The result will be:
[
{'name': 'John', 'cars': [
{'make': 'Audi', 'model_year': 2017},
{'make': 'BMW', 'model_year': 2009},
{'make': 'Volkswagen', 'model_year': 2008}]
},
{'name': 'Mary', 'cars': [
{'make': 'Mazda', 'model_year': 2003}
]
},
{'name': 'Ben', 'cars': [
{'make': 'Ford', 'model_year': 1984}
]
}
]
