Showing posts with label Python Dictionary. Show all posts
Showing posts with label Python Dictionary. Show all posts

Wednesday, July 3, 2019

How to get updated key and value pairs to a new variable in Python Dictionary, but keep the values same for the variables?

Question : if a = {'x': 10 , 'y': 20}
                      b = {'y': 30, ' z': 40},
how to get c = {'x': 10, 'y': 30, 'z': 40} , but a and b should have same key and value pairs?


Answer :
>>> a = {'x': 10 , 'y': 20}
>>> b = {'y': 30, ' z': 40}

>>> c = {}
>>> c.update(a) or c.update(b)

>>> c
{'x': 10, 'y': 30, 'z': 40}
>>> a
{'x': 10, 'y': 20}
>>> b
{'y': 30, 'z': 40}
>>> c
{'x': 10, 'y': 30, 'z': 40}

This will not update a and b , but updates c.

Question solved.