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.
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.
No comments:
Post a Comment