Being a little verbose here to help see the structure. Definitely some cool lambda stuff you can do to solve this and list comprehension to be more "pythonic". But here is a quick solution!
# Set up initial data
unmerged = [
{"id":"1", "name":"Alice", "age":"25", "languages":"German"},
{"id":"1", "name":"Alice", "age":"25", "languages":"French"},
{"id":"2", "name":"John", "age":"30", "languages":"English"},
{"id":"2", "name":"John", "age":"30", "languages":"Spanish"}]
# merge the data by your composite key of id-name-age
merged = {}
for entry in unmerged:
entry_id = entry['id']
entry_name = entry['name']
entry_age = entry['age']
entry_languages = entry['languages']
composite_key = entry_id + entry_name + entry_age
if composite_key in merged:
merged[composite_key]['languages'].append(entry_languages)
else:
merged[composite_key] = {
'id': entry_id,
'name': entry_name,
'age': entry_age,
'languages': [entry_languages]
}
# reconstruct your list with just your unique entries
cleaned = []
for key, value in merged.items():
print(key, value)
cleaned.append({
'id': value['id'],
'name': value['name'],
'age': value['age'],
'languages': ', '.join(value['languages']) # string join langauges by ", "
})
for clean in cleaned:
print(clean)
And than gives you your final output where cleaned is your list of merged entries:
{'id': '1', 'name': 'Alice', 'age': '25', 'languages': 'German, French'}
{'id': '2', 'name': 'John', 'age': '30', 'languages': 'English, Spanish'}
Thank, and let me know if this helps!