Asked 1 years ago
24 Apr 2023
Views 319
steave

steave posted

What is the code to merge two dictionaries into a single expression in Python?


i need to merge two dictionaries into a new dictionary. but dont know how to do it ?
QuickIos

QuickIos
answered Jun 29 '23 00:00

unpacking operator ** will do the job .
** operator is used to unpack dictionaries

dict1 = { **{'c': 3, 'd': 4'} ,'a': 1, 'b': 2' }

now dict1 is 'c': 3, 'd': 4',,'a': 1, 'b': 2'
angeo

angeo
answered Jun 29 '23 00:00

** operator is used to unpack the key-value pairs of two or any number dictionary within a new dictionary literal. The resulting dictionary contains all the key-value pairs from all dictionary.
Post Answer