Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
updating a dictionary
#1
I'm new to python and am having a problem updating a dictionary. I'm trying to enter several key/value pairs in a dictionary, however it appears each update overwrites the previous addition. any one know where I'm going wrong on this?

          ...... 
          print ("tmp_dict before loop:    ", tmp_dict)
          for key, value in tmp_dict.items():
               cisco_vars.update({'interface': {notepad_str: {key: value}}})
               print('cisco_vars in  loop:  ' , cisco_vars,  "\n\n")

          print('cisco_vars after for loop:  ' , cisco_vars,  "\n\n")
          .......
sample output:
Output:
cisco_vars in loop: {'interface': {'FastEthernet1/0/1': {'trunk allowed vlan': ['7']}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars after for loop: {'interface': {'FastEthernet1/0/1': {'trunk allowed vlan': ['7']}}, 'hostname': 'CISCO-ANS-TEST', ....} tmp_dict before loop: {'portfast': True, 'network-policy': '15', 'mode': 'access', 'access_vlan': '144', 'description': 'ADMN-175B-1-2'} cisco_vars in loop: {'interface': {'FastEthernet1/0/2': {'portfast': True}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars in loop: {'interface': {'FastEthernet1/0/2': {'network-policy': '15'}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars in loop: {'interface': {'FastEthernet1/0/2': {'mode': 'access'}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars in loop: {'interface': {'FastEthernet1/0/2': {'access_vlan': '144'}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars in loop: {'interface': {'FastEthernet1/0/2': {'description': 'ADMN-175B-1-2'}}, 'hostname': 'CISCO-ANS-TEST', ....} cisco_vars after for loop: {'interface': {'FastEthernet1/0/2': {'description': 'ADMN-175B-1-2'}}, 'hostname': 'CISCO-ANS-TEST', ....}
final contents of cisco_vars before program termination:
Output:
{'interface': {'FastEthernet1/0/2': {'description': 'ADMN-175B-1-2'}}, 'hostname': 'CISCO-ANS-TEST', ....}
Reply
#2
This is a class I wrote that I use all the time. Makes managing dictionaries (on the create side)
very easy:
It might be of interest: https://github.com/Larz60p/TrulyDynamicDictionary
Reply
#3
(Feb-19-2020, 09:43 PM)evansmb Wrote: I'm trying to enter several key/value pairs in a dictionary, however it appears each update overwrites the previous addition.

Yes, this is the core feature of Python dictionary:

Quote:It is best to think of a dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
/../
If you store using a key that is already in use, the old value associated with that key is forgotten.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating dictionary with tuple Mark17 2 2,014 Aug-06-2020, 02:59 PM
Last Post: Mark17
  Updating dictionary values Sukumar 2 2,446 Oct-03-2018, 09:53 PM
Last Post: Sukumar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020