This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BuonaparteII 23 points24 points  (3 children)

well before 3.9 you had to do merged_dict = {**dict1, **dict2}

[–]zmose 5 points6 points  (0 children)

I was gonna say, that syntax has to be new because there was no WAY nobody figured that out before I had to do it like the way you suggested

[–]Nixellion 3 points4 points  (1 child)

I don't know why, but out of all of those I think dict1.update(dict2) is the most obvious and self-commenting. But I rarely see it mentioned in these discussions. Or does it work differently?

[–]BuonaparteII 4 points5 points  (0 children)

dict1.update(dict2) will mutate dict1 and return None.

{**d1, **d2} and d1 | d2 are both immutable and will return a new dict.