Master Python Dictionary Merging: From Simple Loops to Pandas Grouping
This article walks through transforming a list of Python dictionaries into a consolidated structure using basic loops, itertools grouping, and Pandas, providing clear code examples and visual results to help readers solve common dictionary‑handling challenges.
1. Introduction
Hi, I’m PiPi. In a Python enthusiasts group a member asked how to process a list of dictionaries that contain time, content and a nested speaker list. The original data looks like this:
a = [
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '李明', 'hs': '重庆附属永川'}]},
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '主席:李伟', 'hs': '苏州附属院'}]},
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '王斌', 'hs': '佛山市院'}]}
]The desired output is a single dictionary that merges the speaker entries while keeping the common time and content fields.
2. Basic Implementation
The first solution builds a new dictionary by iterating over the list and appending each speaker entry:
a = [
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '李明', 'hs': '重庆附属永川'}]},
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '主席:李伟', 'hs': '苏州附属院'}]},
{'time': '8:30-9:30', 'content': '开场致词', 'speaker': [{'name': '王斌', 'hs': '佛山市院'}]}
]
new_dict = {}
new_lst = []
for item in a:
new_dict.setdefault('speaker', []).append(item['speaker'])
front_dict = {'time': '8:30-9:30', 'content': '开场致词'}
final_dict = {**front_dict, **new_dict}
print(final_dict)This produces the expected merged structure (see image).
3. Optimized Solutions
Using itertools.groupby and operator.itemgetter the same result can be obtained in a single comprehension:
# This version
from itertools import groupby
from operator import itemgetter
[dict(zip(('time', 'content', 'speaker'),
(*key, sum([i['speaker'] for i in value], []))))
for key, value in groupby(a, itemgetter('time', 'content'))]When Pandas is available, the operation becomes even more concise:
import pandas as pd
pd.DataFrame(a).groupby(['time', 'content']).speaker.sum().reset_index().to_dict(orient='records')4. Summary
The article demonstrates several ways to aggregate nested dictionary data in Python, from straightforward loops to advanced grouping with itertools and pandas, giving readers practical code snippets that can be adapted to similar data‑processing tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
