I had a strange issue while merging 2 dictionaries in Python.
Let's say having a valid empty body for performing ElasticSearch queries:
>>> a = {'aggs': {}, 'query': {'bool': {'must': [], 'should': []}}}
When I print it:
>>> a
{'aggs': {}, 'query': {'bool': {'must': [], 'should': []}}}
Is running perfectly fine.
But when I want to merge another dictionary for filling must
or should
list, the empty one just disappears:
Py 3.9 style:
>>> empty_body | {'query': {'must': [{'match_phrase': {'field': 'value'}}]}}
{'aggs': {}, 'query': {'must': [{'match_phrase': {'field': 'value'}}]}}
Py 3.5 style:
>>> {**empty_body, **{'query': {'must': [{'match_phrase': {'test': 'test'}}]}}}
{'aggs': {}, 'query': {'must': [{'match_phrase': {'test': 'test'}}]}}
In both cases the should
list disappears.
Is there a way to keep empty lists and KISS?