Created
April 21, 2021 01:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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