A summary of four dictionary merging methods in Python

Source: Internet
Author: User
Merging dictionaries (dict) in Python is a common problem. The following article mainly summarizes the introduction of the Python Dictionary (dict) merger of four methods, the need for friends can refer to, the following with the small series to learn together.

This article mainly introduces to you about the Python dictionary (dict) merger four kinds of methods, share out for everyone reference study, words not to say, come together to see the detailed introduction:

A dictionary is the only type of mapping in the Python language.

A hash value (key, key) and a pointer to an object (values, value) are a one-to-many relationship that is often considered a mutable hash table in the mapping type object.

The Dictionary object is mutable, which is a container type that can store any number of Python objects, including other container types.

The difference between a dictionary type and a sequence type:

1. Access to and access to data differs in different ways.

2. The sequence type only uses the key of the numeric type (indexed in numerical order from the beginning of the sequence);

3. The mapping type can be used as a key for other object types (e.g. numbers, strings, ganso, usually strings as keys), and the keys of the sequence type are different, the key of the mapping type is straight

4. Connect to or indirectly associate with the stored data value.

5. The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence type is numerically ordered.

6. The mapping type is directly "mapped" to a value with a key.

Dictionaries are one of the most powerful data types in Python.

In reality, you will often encounter the dictionary merge operation, how to achieve it? Below is a summary of

Method one with the help of Dict (D1.items () + d2.items ())

Note:

1. d1.items() get a list of dictionary key-value pairs

2. d1.items() + d2.items() Spell a new list

3. dict(d1.items()+d2.items()) turn the merged list into a new dictionary

"Method Two" with the help of the Dictionary update () method


"Method Three" with the help of the dictionary dict (d1, **d2) method


"Method Four" with the general method of dictionary


Summarize

Related Article

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.