Python Forum
dict printing last key,value out of loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dict printing last key,value out of loop
#1
Hi There,


        bgp_prefix_dict = {}
        #dict for BGP prefixes
        for line in ip_prefix:
            if 'ip ip-prefix' in line:
                prefix_name = line.strip().split(' ')[2]
                subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
                bgp_subnet = str(IPNetwork(subnet))
                bgp_prefix_dict['prefix_name'] = prefix_name
                bgp_prefix_dict['bgp_subnet'] = bgp_subnet
                #print(bgp_prefix_dict) #### Able to print all key values
        print(bgp_prefix_dict) ## here its printing last key and value
        for (key,value) in bgp_prefix_dict.items():
             print(key, "> ",value)
Output:
{'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
in loop
Output:
prefix_name > MORADABAD_INTERNET_59105832429 bgp_subnet > 103.217.78.0/23
Reply
#2
its not working in for loop, as per my understanding, update will replace key,value if already present.

same result

Output:
{'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
Reply
#3
Your options include at least following methods: check whether key exists and if not create key with value of empty list otherwise append; use .get method; use .setdefault method; use defaultdict.
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
#4
you are updating same keys over and over again, while iterating over the ip_prefix:
bgp_prefix_dict['prefix_name'] = prefix_name
bgp_prefix_dict['bgp_subnet'] = bgp_subnet
bgp_prefix_dict will always have just two keys and their values will be coming from the current element from ip_prefix. At the end of the loop their values will come form last element in ip_prefix.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
will check this, there was some issue while pasting code in last code.. some indent issue

Hi Buran,
I am able to find all key and values in loop, but its printing only last key value out of loop

bgp_prefix_dict = {}
#dict for BGP prefixes
for line in ip_prefix:
    if 'ip ip-prefix' in line:
        prefix_name = line.strip().split(' ')[2]
        subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
        bgp_subnet = str(IPNetwork(subnet))
        bgp_prefix_dict['prefix_name'] = prefix_name
        bgp_prefix_dict['bgp_subnet'] = bgp_subnet
        print(bgp_prefix_dict) #### Able to print all key values
Output:
{'prefix_name': 'Uzaina', 'bgp_subnet': '103.126.62.0/24'} {'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.100.0/22'} {'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.102.0/23'} {'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.101.0/24'} {'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.100.0/22'} {'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.102.0/23'} {'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.101.0/24'} {'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
Reply
#6
(Feb-22-2019, 12:51 PM)anna Wrote: I am able to find all key and values in loop, but its printing only last key value out of loop
Yes, because you overwrite the previous value. What you print proves exactly that!!!!
#list for BGP prefixes dicts
bgp_prefix_list = []
for line in ip_prefix:
    if 'ip ip-prefix' in line:
        #dict for BGP prefixes
        bgp_prefix_dict = {}
        prefix_name = line.strip().split(' ')[2]
        subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
        bgp_subnet = str(IPNetwork(subnet))
        bgp_prefix_dict['prefix_name'] = prefix_name
        bgp_prefix_dict['bgp_subnet'] = bgp_subnet
        print(bgp_prefix_dict) #### Able to print current dict
        bgp_prefix_list.append(bgp_prefix_dict)
print(bgp_prefix_list)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
thanks buran.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 for loop over a dict ogautier 3 1,378 Feb-25-2022, 10:17 AM
Last Post: Larz60+
  Loop Dict with inconsistent Keys Personne 1 1,603 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Sort a dict in dict cherry_cherry 4 73,152 Apr-08-2020, 12:25 PM
Last Post: perfringo
  loop printing twice anna 1 2,524 Sep-04-2019, 09:13 AM
Last Post: anna

Forum Jump:

User Panel Messages

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