Dev Helpers
← Back

Replace only fields explicitly present in a patch

Replace only fields explicitly present in a patch using Python lists and dictionaries.

Open this example in the interactive library

Source code

record = {'name': 'Al', 'note': 'ready', 'rank': 4}
patch = {'note': None, 'rank': 0}
updated = {key: patch.get(key, val) for key, val in record.items()}
print(updated)

Sample output

{'name': 'Al', 'note': None, 'rank': 0}