Dev Helpers
← Back

Attach defaults without sharing nested lists

Attach defaults without sharing nested lists using Python lists and dictionaries.

Open this example in the interactive library

Source code

defaults = {'labels': ['new']}
records = [{'id': 1}, {'id': 2}]
for row in records:
    row['labels'] = defaults['labels'].copy()
records[0]['labels'].append('urgent')
print(records)

Sample output

[{'id': 1, 'labels': ['new', 'urgent']}, {'id': 2, 'labels': ['new']}]