Adding multiple Values to a dictionary key in python?

Viewed 231

I would like to add multiple values to a dictonary key in python. The value is again a dict with some paraments changing..

I have written the following code but that seem to overwrite. any ways to do it effectively?

from collections import defaultdict
node = 'node 1'
d = defaultdict(dict)
d[node] = {'interface':{'Eth1/48':'10.10.10.1'}} --> Here Eth1/48 is dynamically populated.

d[node] = {'interface':{'Eth1/47':'10.10.11.1'}} --> here Eth1/47 is again dynamically populated.
>>> d
defaultdict(<class 'dict'>, {'node 1': {'interface': {'Eth1/47': '10.10.11.1'}}})

I want the output something like this:

{'node 1':{'interface': {'Eth1/48':'10.10.10.1'}
                        {'Eth1/47':'10.10.11.1'}
3 Answers

Make the dict not a dict of dicts, but a dict where a key corresponds to a list of other dicts.

d = defaultdict(dict)
d[node] = {'interface': [{'Eth1/48':'10.10.10.1'}]
d[node]['interface'].append({'Eth1/47':'10.10.11.1'})

Dan's comment below was an excellent one. He pointed out that it makes more sense to have IPs that can correspond to the same key rather than having a list of dicts of Eth*:ip pairs

d = defaultdict(dict)
d[node] = {'interface': {'Eth1/47':['10.10.10.1']}
d[node]['interface']['Eth1/48'] = ['10.10.10.1']

Now you can do

d[node]['interface']['Eth1/47'].append('new.ip.address.ballin')
d[node]['interface']['Eth1/48'].append('new.ip.address.ballin')

This does not exactly match your output, but what I would recommend (as dicts of only a single (key, value) pair are not so useful).

Go one level deeper with a defaultdict(lambda: defaultdict(dict)).

>>> from collections import defaultdict                                                                                
>>>                                                                                                                    
>>> node = 'node 1'                                                                                                    
>>> d = defaultdict(lambda: defaultdict(dict))                                                                         
>>> d[node]['interface']['Eth1/48'] = '10.10.10.1'                                                                     
>>> d[node]['interface']['Eth1/47'] = '10.10.11.1'
>>>                                                                                                                    
>>> d                                                                                                                  
defaultdict(<function __main__.<lambda>()>,
{'node 1': defaultdict(dict,
    {'interface': {'Eth1/47': '10.10.11.1',
                   'Eth1/48': '10.10.10.1'}})})

Change inner dict instead of adding new key to outer one:

d = defaultdict(dict)
d[node] = {'interface':{'Eth1/48':'10.10.10.1'}}
d[node]['interface']['Eth1/47'] = '10.10.11.1'

If you have new dict of values you can add in this way too:

d[node]['interface'] = {**d[node]['interface'], **{'Eth1/47':'10.10.11.1'}}  # joins current d[node]['interface'] and {'Eth1/47':'10.10.11.1'} into one dict
Related