Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do grouping of list of dictionary in Python?

Tags:

I am newbie in Python. I have a list of dictionary which look like as below

[
    {
        'key1': 'value1',
        'key2': [
            {
                'id': 1,
                'name': 'name1'
            },
            {
                'id': 2,
                'name': 'name2'
            },
            {
                'id': 3,
                'name': 'name3'
            }
        ]
    },
    {
        'key1': 'value1',
        'key2': [
            {
                'id': 1,
                'name': 'name1'
            },
        ]
    },
    {
        'key1': 'value1',
        'key2': [
            {
                'id': 1,
                'name': 'name1'
            },
        ]
    }
]

I want to convert this list into lists of list on the basic of common key2-id, The resultant list will look like

[
    [
        {
            'key1': 'value1',
            'key2': [
                {'id': 2, 'name': 'name2'},
                {'id': 3, 'name': 'name3'}]}
    ],
    [
        {
            'key1': 'value1',
            'key2': [{'id': 1, 'name': 'name1'}]
        },
        {
            'key1': 'value1',
            'key2': [{'id': 1, 'name': 'name1'}]
        },
        {
            'key1': 'value1',
            'key2': [{'id': 1, 'name': 'name1'}]}
    ]
]

I have tried to accomplish this using gropby from itertool as below:

from itertools import groupby
def _group_by_key2_ids(results):
    groupby_iterator = groupby(
        results,
        lambda x: [item.get('id') for item in x.get('key2')]
    )
    return [list(x) for _, x in groupby_iterator]

It doesn't give the result that I want. Its output look like

[
    [
        {
            'key1': 'value1',
            'key2': [
                {'id': 1, 'name': 'name1'},
                {'id': 2, 'name': 'name2'},
                {'id': 3, 'name': 'name3'}]}
    ],
    [
        {
            'key1': 'value1',
            'key2': [{'id': 1, 'name': 'name1'}]
        },
        {
            'key1': 'value1',
            'key2': [{'id': 1, 'name': 'name1'}]}]
]

Please let me know, how we can do this. Any help would be appreciated. Thanks.

like image 741
rishi kant Avatar asked Aug 26 '19 10:08

rishi kant


People also ask

How do I sort a list of dictionaries in Python?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.

Can we sort list of dictionary in Python?

We can sort lists, tuples, strings, and other iterable objects in python since they are all ordered objects. Well, as of python 3.7, dictionaries remember the order of items inserted as well. Thus we are also able to sort dictionaries using python's built-in sorted() function.


1 Answers

I'm not sure why exactly ids 2 and 3 stayed together.

here's a version that groups by individual inner dicts (those with id),

I'm not sure if that's helpful or by which logic you group the dicts later on, if you help me wrap my head around it maybe I can help get you desired output:

from itertools import groupby
from pprint import pprint

original = [
    {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}, {'id': 2, 'name': 'name2'}, {'id': 3, 'name': 'name3'}]},
    {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}, ]},
    {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}, ]}
]

keyfunc = lambda x: x['key2'][0]['id']

flattened_and_sorted_by_key2 = sorted([{'key1': d['key1'], 'key2':[inner_d]} for d in original for inner_d in d['key2']], key=keyfunc)

grouped_flattened = [list(x) for _, x in groupby(flattened_and_sorted_by_key2, key=keyfunc)]

pprint(grouped_flattened, indent=2)

this outputs 3 groups, one for each value of id:

[ [ {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}]},
    {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}]},
    {'key1': 'value1', 'key2': [{'id': 1, 'name': 'name1'}]}],
  [{'key1': 'value1', 'key2': [{'id': 2, 'name': 'name2'}]}],
  [{'key1': 'value1', 'key2': [{'id': 3, 'name': 'name3'}]}]]
like image 170
Adam.Er8 Avatar answered Oct 12 '22 20:10

Adam.Er8