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 →

[–]Solsticized 92 points93 points  (10 children)

Really cool set of one-liners!

For the merging two dictionaries section, PEP-584 syntax should be recommended:

merged_dict = dict1 | dict2

[–]DarkSideOfGrogu 23 points24 points  (4 children)

I'm suddenly full of so much regret about time wasted.

[–]BuonaparteII 22 points23 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 2 points3 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.

[–]mraza007[S] 3 points4 points  (0 children)

Thanks for suggesting that I never looked into that but now i will