This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
51
Merge dictionaries with {**a, **b} (in Python 3.5+)
Post Flair (click to view more posts with a particular flair)
Post Body
Example:
>>> stuff = {'duck': 4, 'spam': 2}
>>> more_stuff = {'ribbon': 3, 'egg': 1}
>>> all_the_stuff = {**stuff, **more_stuff}
>>> all_the_stuff
{'egg': 1, 'duck': 4, 'spam': 2, 'ribbon': 3}
This is functionally equivalent to:
>>> all_the_stuff = {}
>>> all_the_stuff.update(stuff)
>>> all_the_stuff.update(more_stuff)
>>> all_the_stuff
{'egg': 1, 'duck': 4, 'spam': 2, 'ribbon': 3}
This feature came out of PEP 448.
This is actually a nudge to encourage everyone to upgrade to the latest Python 3.5! I know that not everyone can upgrade due to work difficulties and legacy code, but hopefully this feature will make you consider taking up that battle a little sooner.
If you cannot upgrade to Python 3.5, I wrote about a few other ways you can merge dictionaries in older versions of Python.
Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
3,345
Link Karma
2,456
Comment Karma
889
Profile updated: 2 days ago
Posts updated: 8 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 8 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/pythontips/...