0

I am newbie to Python . Need help in joining/merging the output of a for loop which creates multiple dictionaries into one single big dictionary .

        vpc_peer = {} 
        for key in vpc_list: 
            for value in neigh_ips: 
                vpc_peer[key] = value 
                neigh_ips.remove(value)  
            break           
        print(vpc_peer)

The print output looks like this

{'vpc-0a21601da78d1ef30': '169.254.27.159'}

{'vpc-049920287c06fb681': '169.254.27.203'}

{'vpc-0f3bc5629259fb713': '169.254.28.23'}

{'vpc-0b0e5a878de4ca0b3': '169.254.28.27'}

What I am looking for is to merge the output of the for loop into one big dictionary as below

{'vpc-0a21601da78d1ef30': '169.254.27.159','vpc-049920287c06fb681': '169.254.27.203', 'vpc-0f3bc5629259fb713': '169.254.28.23','vpc-0b0e5a878de4ca0b3': '169.254.28.27' }

Troubadour
  • 13,334
  • 2
  • 38
  • 57

1 Answers1

0

I think this question has many possible answers to your problem. A quick way to do this is just to use the update() function:

vpc_peer.update(neigh_ips)
Guilherme B.M.
  • 65
  • 1
  • 10
  • Thanks for the response . Can you tell me where exactly to use this in the code ? Because when I am trying , I am not getting the intended result . The "for loop" is output is printing out 200 plus dictionaries one per line which I need to merge into a single dictionary . – benin grint Feb 21 '21 at 12:57
  • I think maybe I did not understand completely your problem. The piece of code you show in your question is actually inside another for loop? If that's the case, can you show the rest of the code as well too? My idea was to just use: ```vcc_peer = {}``` then do ```vpc_peer.update(neigh_ips)``` and nothing else. – Guilherme B.M. Feb 22 '21 at 14:37
  • That's ok , I figured out a way to get it done . – benin grint Mar 01 '21 at 13:49