Skip to content

Instantly share code, notes, and snippets.

@alecbw
Created April 21, 2021 01:48
Show Gist options
  • Save alecbw/e293cabbd9f86ea18290ea86a0522e1c to your computer and use it in GitHub Desktop.
Save alecbw/e293cabbd9f86ea18290ea86a0522e1c to your computer and use it in GitHub Desktop.
Combine two lists of dictionaries into one list of dictionaries, where a primary_key is used to join the dictionaries between them
from collections import defaultdict
"""
Zip is at the dict level - if only some of the dicts in a lod have a key,
only resultant dicts with one of their primary_keys will have that given k:v pair
When both lods have a given (non-primary) key, the lod_2 value is prioritized.
Originally from https://stackoverflow.com/questions/5501810/join-two-lists-of-dictionaries-on-a-single-key
"""
def zip_lods(lod_1, lod_2, primary_key, **kwargs):
d = defaultdict(dict)
for l in (lod_1, lod_2):
for elem in l:
if kwargs.get("keys_subset_list") and primary_key in kwargs['keys_subset_list']:
elem = {k:v for k,v in elem.items() if k in kwargs['keys_subset_list']}
elif kwargs.get("keys_subset_list"):
raise ValueEror("Check your keys_subset - it needs to have the primary_key and be a list")
d[elem[primary_key]].update(elem)
output_lod = list(d.values())
return output_lod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment