Python3 Quick Merge Dictionary

Source: Internet
Author: User

When there are two dictionaries that need to be merged, take into account the particularity of the dictionary, you need to first turn it into the form of a list, add operations and then back to the dictionary, as follows:

x = {' A ': 1, ' B ': 3}y = {' C ': 5, ' d ': 8}z = dict (List (X.items ()) + list (Y.items ())) print (z)
# {' d ': 8, ' C ': 5, ' a ': 1, ' B ': 3}

A more convenient method is available in the python3.5:

x = {' A ': 1, ' B ': 3}y = {' C ': 5, ' d ': 8} #z = Dict (List (X.items ()) + list (Y.items ())) Z = {**x, **y}print (z)

The result is the same as above, and faster (do not know the principle, just look at the data said faster), this method is Python has wood?

Python3 Quick Merge Dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.