3

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 3

1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Since the interface ID is the key and the address is the value, I think it makes less sense to put each interfce key/value pair in a different list element. Better to combine to one dict with key of interface ID and value of address in my opinion. Interfaces can hold more than one address so the place for the list, I'd say, is in the dictionary value {'Eth1/48':['10.0.0.1', '10.10.10.1']}
I believe ingvar's answer is the closest to what OP wanted and would go with that. I gave a way in how I would do it for multiple values for the same key.
Dan, that is an excellent Idea. I'll edit my answers to note what you have said. I agree with your comment. It is a better idea.
1

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'}})})

Comments

0

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

1 Comment

or you could define the inner dict all at once: {'interface':{'Eth1/48':'10.10.10.1', 'Eth1/47': '10.10.11.1'}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.