Search code examples
hy

Dictionary merging in Hy


If I have two dictionaries, and want to merge them with preference to the latter regarding conflicting keys, I would do in Python:

In [1]: x = {'a': 1, 'b': 2}

In [2]: y = {'b': 3, 'c': 4}

In [3]: {**x, **y}
Out[3]: {'a': 1, 'b': 3, 'c': 4}

How can I write this expression in Hy's syntax?


Solution

  • The dictionary-unpacking operator is unpack-mapping, or #** for short.

    => (setv x {"a" 1  "b" 2})
    => (setv y {"b" 3  "c" 4})
    => {#** x  #** y}
    {"a" 1  "b" 3  "c" 4}