Python merges two dictionaries, and python merges two dictionaries.

Source: Internet
Author: User

Python merges two dictionaries, and python merges two dictionaries.

Two Methods: update () and items ()

In [14]: aOut [14]: {'A': 1, 'B': 2, 'C': 3} In [15]: c = {'D': 4} In [16]:. update (c) In [17]: aOut [17]: {'A': 1, 'B': 2, 'C': 3, 'D ': 4} In [18]: a = {'A': 1} In [19]: aOut [19]: {'A': 1} In [20]: cOut [20]: {'D': 4} In [21]:. items () + c. items () Out [21]: [('A', 1), ('D', 4)] In [22]: dict (. items () + c. items () Out [22]: {'A': 1, 'D': 4}

In [29]: z = a. update (B) In [30]: z

It is not allowed to assign values directly to new variables, so z gets a null value. here we need to look at the update usage,

See http://www.tutorialspoint.com/python/dictionary_update.htm

This function does not return any value, orz, so there is no value

 

It can also be used to update the value in the dictionary.

In [35]:. update ({'A': 2}) In [36]: aOut [36]: {'A': 2, 'C': 3} In [37]:. update ({'A': 3, 'D': 'D'}) In [38]: aOut [38]: {'A': 3, 'C ': 3, 'D': 'D '}

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.