- Curly brackets to indicate the “merged” is a dictionary
- (double star **) that indicates the multiple entries are coming but not sure how many
- same applied to dict2
Once merged is created and turned into a dict object it will simply grab an entry from dict1, followed by dict2, etc). Only unique k,v pairs are recorded. If for example dict1 has an entry “a” : 2. And the dict2 has an entry that says “a” : 6. The final dictionary will only show key a with value 6. The “old” entry where the key was “a” has been simply reassigned a new value of 6
Hi,
I’m new to Python and came across the following line in a solution:
"merged = {**dict1, **dict2}"
I’m having trouble understanding how this works internally. Could you please help me understand how Python evaluates this expression?
Thank you for your time and assistance.
Hi Bijal,
“merged” is just the name of the new Dict object.
The right hand side is where things happen:
- Curly brackets to indicate the “merged” is a dictionary
- (double star **) that indicates the multiple entries are coming but not sure how many
- same applied to dict2
Once merged is created and turned into a dict object it will simply grab an entry from dict1, followed by dict2, etc). Only unique k,v pairs are recorded. If for example dict1 has an entry “a” : 2. And the dict2 has an entry that says “a” : 6. The final dictionary will only show key a with value 6. The “old” entry where the key was “a” has been simply reassigned a new value of 6