Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping based on the dictionary keys

I have a Dictionary as below:

    dict_details = {
        'device.dummy.1.id': '1',
        'device.dummy.2.id': '2',
        'device.dummy.1.length': '202',
        'device.dummy.2.length': '203',
        'device.dummy.2.validity': '10001',
        'device.dummy.1.validity': '10002',
        'device.dummy.1.type': 'first',
        'device.dummy.2.type': 'first'
    }

I need to create a following dictionary with the above dictionary. That is group the dictionary values with some unique identifier which will be present in my dict.

{
    'device.dummy.1': {
        'id': '1',
        'length': '202',
        'validity': '10001',
        'type': 'first'
    },
    'device.dummy.2': {
        'id': '2',
        'length': '203',
        'validity': '10002',
        'type': 'first'
    }
}

I tried the following code:

    dict_details = {
        'device.dummy.1.id': '1',
        'device.dummy.2.id': '2',
        'device.dummy.1.length': '202',
        'device.dummy.2.length': '203',
        'device.dummy.2.validity': '10001',
        'device.dummy.1.validity': '10002',
        'device.dummy.1.type': 'first',
        'device.dummy.2.type': 'first'
    }
    s = {}
    dummy = {}
    for key, value in dict_details.items():

        attrib = key.rsplit(".",1)[1]
        macro = key.rsplit(".",1)[0]
        s[attrib] = value
        dummy[macro] = s
    print(dummy)

But It is generating a output as below, That is key's value getting overwritten by the already existing keys.

Looking for the some simple workaround for the same.

{
    'device.dummy.1': {
        'length': '202',
        'type': 'first',
        'id': '1',
        'validity': '10001'
    },
    'device.dummy.2': {
        'length': '202',
        'type': 'first',
        'id': '1',
        'validity': '10001'
    }
}
like image 348
sathis Avatar asked Feb 17 '26 11:02

sathis


1 Answers

With defaultdict

You can achieve this with a defaultdict:

from collections import defaultdict
dict_details = {
        'device.dummy.1.id': '1',
        'device.dummy.2.id': '2',
        'device.dummy.1.length': '202',
        'device.dummy.2.length': '203',
        'device.dummy.2.validity': '10001',
        'device.dummy.1.validity': '10002',
        'device.dummy.1.type': 'first',
        'device.dummy.2.type': 'first'
    }

new_dict = defaultdict(dict)

for key in dict_details:
    a, b = key.rsplit(".", 1)
    new_dict[a][b] = dict_details[key]

print(new_dict)
# defaultdict(<type 'dict'>, {'device.dummy.1': {'length': '202', 'type': 'first', 'id': '1', 'validity': '10002'}, 'device.dummy.2': {'length': '203', 'type': 'first', 'id': '2', 'validity': '10001'}})

With standard dicts

Without defaultdict, you'd just need to check if the subdict is defined before adding attributes:

dict_details = {
      'device.dummy.1.id': '1',
      'device.dummy.2.id': '2',
      'device.dummy.1.length': '202',
      'device.dummy.2.length': '203',
      'device.dummy.2.validity': '10001',
      'device.dummy.1.validity': '10002',
      'device.dummy.1.type': 'first',
      'device.dummy.2.type': 'first'
  }

dummy = {}

for key, value in dict_details.items():
    attrib, macro = key.rsplit(".",1)
    if not dummy.get(attrib):
        dummy[attrib] = {}
    dummy[attrib][macro] = value

print(dummy)
# {'device.dummy.1': {'length': '202', 'type': 'first', 'id': '1', 'validity': '10002'}, 'device.dummy.2': {'length': '203', 'type': 'first', 'id': '2', 'validity': '10001'}}
like image 179
Eric Duminil Avatar answered Feb 20 '26 00:02

Eric Duminil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!