如何更高效实现字典列表合并?无需双层循环的方案探讨
Great question! Your current approach gets the job done, but we can absolutely streamline this to avoid explicit nested loops, cut down on unnecessary overhead, and make the code cleaner and more efficient. Here are a couple of improved approaches:
方法1:利用推导式与集合优化(最简洁高效)
This approach leverages Python's optimized list and dictionary comprehensions, plus sets for fast key deduplication—no more nested loops with try/except blocks:
from itertools import chain def merge_dicts(list_of_dicts: list, missval=None): '''Merges a list of dicts, having common keys into a single dict with items appended to a list >>> d1 = {'a' : 1, 'b': 2, 'c': 3} >>> d2 = {'a':4, 'b':5 } >>> d3 = {'d': 5} >>> merge_dicts([d1, d2, d3], 'NA') {'a': [1, 4, 'NA'], 'b': [2, 5, 'NA'], 'c': [3, 'NA', 'NA'], 'd': ['NA', 'NA', 5]} ''' # 用集合快速收集所有唯一键,比原列表判断高效得多 all_keys = set(chain.from_iterable(list_of_dicts)) # 字典推导式+列表推导式完成合并,逻辑清晰且执行速度快 return { key: [d.get(key, missval) for d in list_of_dicts] for key in all_keys }
为什么这比原实现更好?
- 更快的键去重:原代码用列表存储键并逐个判断
k not in all_keys,时间复杂度是O(n²);改用集合后,去重操作是O(n)(集合的成员检查是O(1))。 - 无异常处理开销:原代码依赖
try/except来初始化列表,这会带来额外的运行时开销;推导式直接构建列表,完全避免了这个问题。 - 更简洁可读:核心逻辑浓缩在几行里,一目了然,维护起来更轻松。
方法2:用defaultdict简化循环(可选)
If you prefer a more explicit loop-based approach but still want to avoid try/except blocks, collections.defaultdict is a great tool:
from itertools import chain from collections import defaultdict def merge_dicts(list_of_dicts: list, missval=None): all_keys = set(chain.from_iterable(list_of_dicts)) merged = defaultdict(list) for key in all_keys: for d in list_of_dicts: merged[key].append(d.get(key, missval)) return dict(merged)
This eliminates the KeyError handling by initializing each key's value as an empty list automatically, and still uses sets for efficient key collection.
Both approaches will produce the exact same output as your original function, but with better performance and cleaner code.
内容的提问来源于stack exchange,提问作者Abhishek Bhatia

