yan
yan

Reputation: 649

Reorder and return the whole of nested dictionary

I am trying to retain the whole contents of a nested dictionary but only with its contents reordered..

This is an example of my nested dictionaries (pardon the long example..) -

{
    "pages": {
        "rotatingTest": {
            "elements": {
                "apvfafwkbnjn2bjt": {
                    "name": "animRot_tilt40_v001", 
                    "data": {
                        "description": "tilt testing", 
                        "project": "TEST", 
                        "created": "26/11/18 16:32", 
                    }, 
                    "type": "AnimWidget", 
                    "uid": "apvfafwkbnjn2bjt"
                }, 
                "p0pkje1hjcc9jukq": {
                    "name": "poseRot_positionD_v003", 
                    "data": {
                        "description": "posing test for positionD", 
                        "created": "10/01/18 14:16", 
                        "project": "TEST", 
                    }, 
                    "type": "PosedWidget", 
                    "uid": "p0pkje1hjcc9jukq"
                }, 
                "k1gzzc5uy1ynqtnj": {
                    "name": "animRot_positionH_v001", 
                    "data": {
                        "description": "rotational posing test for positionH", 
                        "created": "13/06/18 14:19", 
                        "project": "TEST", 
                    }, 
                    "type": "AnimWidget", 
                    "uid": "k1gzzc5uy1ynqtnj"
                }
            }
        }, 
        "panningTest": {
            "elements": {
                "7lyuri8g8u5ctwsa": {
                    "name": "posePan_positionZ_v001", 
                    "data": {
                        "description": "panning test for posZ", 
                        "created": "04/10/18 12:43", 
                        "project": "TEST", 
                    }, 
                    "type": "PosedWidget", 
                    "uid": "7lyuri8g8u5ctwsa"
                }
            }
        }, 
        "zoomingTest": {
            "elements": {
                "prtn0i6ehudhz475": {
                    "name": "posZoom_positionH_v010", 
                    "data": {
                        "description": "zoom test", 
                        "created": "11/10/18 12:42", 
                        "project": "TEST", 
                    }, 
                    "type": "PosedWidget", 
                    "uid": "prtn0i6ehudhz475"
                }
            }
        }
    }, 
    "page_order": [
        "rotatingTest", 
        "zoomingTest", 
        "panningTest"
    ]
}

and this is my code:

for k1, v1 in test_dict.get('pages', {}).items():
    return (sorted(v1.get('elements').items(), key=lambda (k2,v2): v2['data']['created']))

In the code, keys such as the page_order, pages etc are missing... Or if there is/ are any commands where it will enables me to retain the 'whole' of the dictionary?

Appreciate in advance for any advice.

Upvotes: 0

Views: 136

Answers (1)

ic3b3rg
ic3b3rg

Reputation: 14927

If you're using Python 3.7, a dict will preserve insert order. Otherwise, you need to use an OrderedDict.Additionally, you need to convert the date string to a date to get the correct sort order:

from datetime import datetime

def sortedPage(d):
    return {k: {'elements': dict(sorted(list(v['elements'].items()), key=lambda tuple: datetime.strptime(tuple[1]['data']['created'], '%d/%m/%y %H:%M')))} for k,v in d.items()}

output = {k: sortedPage(v) if k == 'pages' else v for k,v in input.items()}

Upvotes: 1

Related Questions