Home > Net >  Cannot Append new list data to a dict in a for loop
Cannot Append new list data to a dict in a for loop

Time:06-03

I have a list of IPs being returned from a POST in listip. I want to iterate over the list of IPs and store data in a dictionary so i can render it on a webpage. But the dictionary is overriding the values for the last IP only. How can i solve this ? Currently there are 3 IPs in the listip but dict is only storing the last passed IPs data.

def healthcheckresults(request):
if not listip:
    return render(request, "home/homepage.html",)
for ip in range(len(listip)):
    conn = manager.connect(
    host= listip[ip],
    port='22',
    username='XXX',
    password = 'XXX',
    timeout=10
    )
    result = conn.get_ospf_neighbor_information()
    hostnameresult = conn.get_software_information()
    hostname = hostnameresult.xpath('//software-information/host-name/text()')
    ospfneighboraddress = result.xpath('//ospf-neighbor/neighbor-address/text()')
    ospfneighborinterface = result.xpath('//ospf-neighbor/interface-name/text()')
    ospfneighborstate= result.xpath('//ospf-neighbor/ospf-neighbor-state/text()')
    ospfneighborID = result.xpath('//ospf-neighbor/neighbor-id/text()')
    
    ##METHOD1
    ospfdictkey = {"hostname":[],"ospfneighboraddress":[],"ospfneighborinterface":[],"ospfneighborstate":[],"ospfneighborID":[]}
    ospfmetalist = [hostname,ospfneighboraddress,ospfneighborinterface,ospfneighborstate,ospfneighborID]
    for key, value in zip(ospfdictkey, ospfmetalist):
        ospfdictkey[key].append(value)
        
    ##METHOD2
    ospfdict={"hostname":hostname,"ospfneighboraddress":ospfneighboraddress,"ospfneighborinterface":ospfneighborinterface, "ospfneighborstate":ospfneighborstate,"ospfneighborID":ospfneighborID }
    context = {'LUnique': zip(ospfneighboraddress, ospfneighborinterface, ospfneighborstate,ospfneighborID)}
    conn.close_session()

listip.clear()
return render(request, "healthcheck/healthcheckresults.html",{
    "ospfneighboraddress":ospfneighboraddress,
    "ospfneighborinterface":ospfneighborinterface,
    "ospfneighborstate":ospfneighborstate,
    "ospfneighborID":ospfneighborID,
    "context":context,
    "hostname":hostname,
    "listip":listip,
    "ospfdict":ospfdict,
    "ospfdictkey":ospfdictkey,
})

Both mentioned methods are returning the same data when i check the data in the dictionary.

{'hostname': ['R3-ISP'], 'ospfneighboraddress': ['192.168.5.34', '192.168.5.5', '192.168.5.10'], 'ospfneighborinterface': ['ae10.0', 'ae2.0', 'ae3.0'], 'ospfneighborstate': ['Full', 'Full', 'Full'], 'ospfneighborID': ['172.0.0.6', '172.0.0.2', '172.0.0.4']}

{'hostname': [['R3-ISP']], 'ospfneighboraddress': [['192.168.5.34', '192.168.5.5', '192.168.5.10']], 'ospfneighborinterface': [['ae10.0', 'ae2.0', 'ae3.0']], 'ospfneighborstate': [['Full', 'Full', 'Full']], 'ospfneighborID': [['172.0.0.6', '172.0.0.2', '172.0.0.4']]} ['R3-ISP']

CodePudding user response:

In each method you are overwriting the dict. Remember that the code is repeating in each iteration of

for ip in range(len(listip)):

Which means that when you set the keys and values in the first line of each method you are overwriting any dict of that name that has gone before.

One way to avoid this would be to create an empty list, and append each new dict to it upon creation. Then you can cycle through the list to see each dict.

aList = []
for ip in range(len(listip)):
...
#Method1
...
aList.append(ospfdictkey )
#Method2
...
aList.append(ospfdict)
  • Related