Skip to content Skip to sidebar Skip to footer

Sort And Group A List Of Dictionaries

How can I sort and group this list of dictionaries into a nested dictionary which I want to return via an API as JSON. Source Data (list of permissions): [{ 'can_create': True,

Solution 1:

TADA!

from itertools import groupby

defgroup_by_remove(permissions, id_key, groups_key, name_key=None):
    """
    @type permissions: C{list} of C{dict} of C{str} to C{object}
    @param id_key: A string that represents the name of the id key, like "role_id" or "module_id"
    @param groups_key: A string that represents the name of the key of the groups like "modules" or "permissions"
    @param name_key: A string that represents the name of the key of names like "module_name" (can also be None for no names' key) 
    """
    result = []
    permissions_key = lambda permission: permission[id_key]
    # Must sort for groupby to work properly
    sorted_permissions = sorted(permissions, key=permissions_key)
    for key, groups in groupby(sorted_permissions, permissions_key):
        key_result = {}
        groups = list(groups)
        key_result[id_key] = key
        if name_key isnotNone:
            key_result[name_key] = groups[0][name_key]
        key_result[groups_key] = [{k: v for k, v in group.iteritems() if k != id_key and (name_key isNoneor k != name_key)} for group in groups]
        result.append(key_result)
    return result

defchange_format(initial):
    """
    @type initial: C{list}
    @rtype: C{dict} of C{str} to C{list} of C{dict} of C{str} to C{object}
    """
    roles_group = group_by_remove(initial, "role_id", "modules")[0]
    roles_group["modules"] = group_by_remove(roles_group["modules"], "module_id", "permissions", "module_name")
    return roles_group

change_format(role_permissions)

Enjoy :)

Solution 2:

PyFunctional is pretty good at list manipulation.

from pprint import pprint
from functional import seq
input = [...]  # taken from your example
output =(
    seq(input)  # convert regular python list to Sequence object# group by role_id
    .map(lambda e: (e.pop('role_id'), e)).group_by_key()

    # start building role dict
    .map(lambda role_modules: {
        "role_id": role_modules[0],
        "modules": seq(role_modules[1])
                   # group by (module_id, module_name)
                   .map(lambda e: ( (e.pop('module_id'), e.pop('module_name')), e) ).group_by_key()

                   # start building module/permissions dict
                   .map(lambda module_permissions: {
                       "module_id": module_permissions[0][0],
                       "module_name": module_permissions[0][1],
                       "permissions": module_permissions[1]
                   })
                   # sort by module_id, convert Seq obj to regular list
                   .sorted(key=lambda m:m['module_id']).to_list()
    })
    # sort by role_id, convert Seq obj to regular list
    .sorted(key=lambda r:r['role_id']).to_list()
)

pprint(output)

RESULT

[{'modules': [{'module_id': 1,
               'module_name': 'ModuleOne',
               'permissions': [{'can_create': True,
                                'can_delete': True,
                                'can_read': True,
                                'can_update': True,
                                'end_point_id': 1,
                                'end_point_name': 'entity'},
                               {'can_create': True,
                                'can_delete': True,
                                'can_read': True,
                                'can_update': True,
                                'end_point_id': 2,
                                'end_point_name': 'management-type'},
                               {'can_create': True,
                                'can_delete': False,
                                'can_read': True,
                                'can_update': True,
                                'end_point_id': 3,
                                'end_point_name': 'ownership-type'}]},
              {'module_id': 2,
               'module_name': 'ModuleTwo',
               'permissions': [{'can_create': True,
                                'can_delete': True,
                                'can_read': True,
                                'can_update': True,
                                'end_point_id': 4,
                                'end_point_name': 'financial-outlay'},
                               {'can_create': True,
                                'can_delete': True,
                                'can_read': True,
                                'can_update': True,
                                'end_point_id': 5,
                                'end_point_name': 'exposure'}]}],
  'role_id': 1}]

Post a Comment for "Sort And Group A List Of Dictionaries"