Skip to content

optional merge strategies for lists/sets #10

@kbroughton

Description

@kbroughton

The standard library defines what is meant by update for two dicts.
For other container types, there may be several options.
Currently on d1.update(d2) if the value is a list then d2's list over-writes d1's list.

I would like to have the option of specifying the behaviour for lists and sets.
For example, i would like the lists here to merge by appending
d1 = {'a':1,'f':[1,3]}
d2 = {'a':1,'f':[1,2]}
d1.update(d2) ->
{'a':1,'f':[1,3,1,2]}

Or i might prefer my lists to uniquely append
d1.update(d2) ->
{'a':1,'f':[1,3,2]}

Or as in this S.O. http://stackoverflow.com/questions/1319338/combining-two-lists-and-removing-duplicates-without-removing-duplicates-in-orig

This could be done if update took a strategies parameter, perhaps a dict mapping types to combiners.
strategies=[{'name': 'unique_extend_list', 'signature': ('list','list'): 'combiner': lambda: x,y: x + list(set(y) - set(x)) },]
And then modify the _recursive_update to handle the cases.

Have you considered something like this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions