gmr / flatdict

Python module for interacting with nested dicts as a single level dict with delimited keys.
https://flatdict.readthedocs.io
BSD 3-Clause "New" or "Revised" License
112 stars 32 forks source link

Empty list [] in FlatterDict converts to empty dictionary {} #54

Open ttamg opened 1 year ago

ttamg commented 1 year ago

This is also related to issue #43 but in the case where our nested dictionary has empty lists []

In this case, not only do we have the empty <FlatterDict {}>, but in the flat form it encodes the empty list as an empty dictionary instead.

In the original as_dict() form it is correct as you would expect. The issue is in the flattened form.

Here is a simple example

dict_ = {"age": 25, "siblings": []}

flat = flatdict.FlatterDict(dict_)

print(flat.as_dict())

print([(k, v) for k, v in flat.items()])

print(flat['siblings'])

Outputs

{'age': 25, 'siblings': []}

[('age', 25), ('siblings', <FlatterDict id=5520522448 {}>")]

{}    # wrong should be []
oalfonso-o commented 1 year ago

bump

tgh19 commented 12 months ago

I have a PR out that should fix this https://github.com/gmr/flatdict/pull/57

print(FlatDict({'a': {}, 'b': []}).as_dict())
{'a': {}, 'b': []}
DomagojTadin commented 3 months ago

bump