question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unflatten with lists

See original GitHub issue

Flattening a nested dict that contains lists works great, but unflatten makes dicts instead of lists when index is list index. I rewrote part of your lib to unflatten for my needs and thought you might want to integrate it into you unflatten.

I’m worried that my changes aren’t generic enough work for all kinds of mixed list with dict.

Here is I how did the unflattening. The only function I change is this one:

def nested_set_dict(d, keys, value):
    """Set a value to a sequence of nested keys

    Parameters
    ----------
    d : Mapping
    keys : Sequence[str]
    value : Any
    """
    assert keys
    key = keys[0]
    if len(keys) == 1:
        if type(d) == list:
            d.append(value)
        else:
            d[key] = value
        return

    # the type is a string so make a dict if none exists
    if type(keys[1]) == int:
        if key in d:
            pass
        else:
            d[key] = []
        d = d[key]
    elif type(key)==int:
        if (key+1) > len(d):
            d.append({})
        d = d[key]
    else:
        d = d.setdefault(key, {})
    nested_set_dict(d, keys[1:], value)

Testing it out:

d1 = {'a':{'b':[{'c1':'nested1!','d1':[{'e1':'so_nested1!!!'}]},
               {'c2':'nested2!','d2':[{'e2':'so_nested2!!!'}]},
               {'c3':'nested3!','d3':[{'e3':'so_nested3!!!'}]},
               {'c4':'nested4!','d4':[{'e4':'so_nested4a!!!'},
                                      {'e4':'so_nested4b!!!'},
                                      {'e4':'so_nested4c!!!'},
                                      {'e4':'so_nested4d!!!'},
                                      {'e4':'so_nested4e!!!'}]}]}}    

Flatten works great for this out of the box

df = mzm.flatten(d1,enumerate_types=(list,))
kv = sorted([(k,v) for (k,v) in df.items()])

((‘a’, ‘b’, 0, ‘c1’), ‘nested1!’) ((‘a’, ‘b’, 0, ‘d1’, 0, ‘e1’), ‘so_nested1!!!’) ((‘a’, ‘b’, 1, ‘c2’), ‘nested2!’) ((‘a’, ‘b’, 1, ‘d2’, 0, ‘e2’), ‘so_nested2!!!’) ((‘a’, ‘b’, 2, ‘c3’), ‘nested3!’) ((‘a’, ‘b’, 2, ‘d3’, 0, ‘e3’), ‘so_nested3!!!’) ((‘a’, ‘b’, 3, ‘c4’), ‘nested4!’) ((‘a’, ‘b’, 3, ‘d4’, 0, ‘e4’), ‘so_nested4a!!!’) ((‘a’, ‘b’, 3, ‘d4’, 1, ‘e4’), ‘so_nested4b!!!’) ((‘a’, ‘b’, 3, ‘d4’, 2, ‘e4’), ‘so_nested4c!!!’) ((‘a’, ‘b’, 3, ‘d4’, 3, ‘e4’), ‘so_nested4d!!!’) ((‘a’, ‘b’, 3, ‘d4’, 4, ‘e4’), ‘so_nested4e!!!’)

d2 = {}
for key_value in kv:
    k = key_value[0]
    v = key_value[1]
    nested_set_dict(d2,k,v)

Gives

d1 =

{'a': {'b': [{'c1': 'nested1!', 'd1': [{'e1': 'so_nested1!!!'}]}, {'c2': 'nested2!', 'd2': [{'e2': 'so_nested2!!!'}]}, {'c3': 'nested3!', 'd3': [{'e3': 'so_nested3!!!'}]}, {'d4': [{'e4': 'so_nested4a!!!'}, {'e4': 'so_nested4b!!!'}, {'e4': 'so_nested4c!!!'}, {'e4': 'so_nested4d!!!'}, {'e4': 'so_nested4e!!!'}], 'c4': 'nested4!'}]}}

d2 =

{'a': {'b': [{'c1': 'nested1!', 'd1': [{'e1': 'so_nested1!!!'}]}, {'c2': 'nested2!', 'd2': [{'e2': 'so_nested2!!!'}]}, {'c3': 'nested3!', 'd3': [{'e3': 'so_nested3!!!'}]}, {'d4': [{'e4': 'so_nested4a!!!'}, {'e4': 'so_nested4b!!!'}, {'e4': 'so_nested4c!!!'}, {'e4': 'so_nested4d!!!'}, {'e4': 'so_nested4e!!!'}], 'c4': 'nested4!'}]}}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
HoernchenJcommented, Sep 3, 2021

@ianlini & @ori-levi Hello, are there any updates to this planed feature?

1reaction
ori-levicommented, Jan 14, 2021

@ianlini I just finish do develop the suggested solution, with JSONPath. Note that only JSONPath is reverseable.

I reformatted my code and write some test and open pull request for this. I hope to do this before Sunday.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unflattening a list in python - Stack Overflow
Write a function named "unflatten" which takes a list as an argument and constructs a nested list. The format of the argument list...
Read more >
"Unflattening" a list - Mathematica Stack Exchange
You can unflatten every list with the same number of elements as long as you still have your original list . When you...
Read more >
Unflatten a list (Easy) - Codewars
So you have to build a method, that creates new arrays, that can be flattened! #Shorter: You have to unflatten a list/an array....
Read more >
Python: How to Flatten a List of Lists - Stack Abuse
Flattening a list of lists entails converting a 2D list into a 1D list by un-nesting each list item stored in the list...
Read more >
unflatten - PyPI
This package provides a function which can unpack a flat dictionary into a structured dict with nested sub-dicts and/or sub-lists. Development takes place...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found