Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of merging dictionaries in Python

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the methods of merging dictionaries in Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the methods of merging dictionaries in Python.

Python dictionary

Dictionary is a unique data structure in Python. It contains multiple elements, each of which is a key-value pair. For example, initialize a dictionary D1 that contains two elements. The value of the key "name" is "Tom" and the value of the key "age" is 20.

D1 = {'name':' Tom', 'age': 20}

The dictionary stores information about 20-year-old Tom.

Suppose for some reason we collected more information about Tom, such as his GPA and marital status. You can now create another dictionary called D2.

D2 = {'gpa': 4.0,' is_single': True}

Now I want to merge the two dictionaries together because they contain different information about the same person (Tom).

So the question is, how do you merge two dictionaries in Python?

1. Stupid method

You can insert a new element into an existing dictionary using the assignment operator "=" in the statement dict_ name [key] = value.

D1 = {'name':' Tom', 'age': 20} D1 [' sex'] = 'Male'# D1 = = {' name': 'Tom',' age': 20, 'sex':' Male'}

Therefore, without using any particular dictionary method, the first method that comes to mind is to write a for loop, iterate over each key-value pair using iterable. Items (), and then insert the pair into the new dictionary dnew.

D1 = {'name':'Tom',' age': 20} D2 = {'gpa': 4.0,' is_single': True} dnew = dict () for key, value in d1.items (): dnew [key] = value for key, value in d2.items (): dnew [key] = value# dnew = {' name':'Tom', 'age': 20,' gpa': 4.0, 'is_single': True}

However, the merge dictionary should be very simple and straightforward and should be implemented with a single line of code.

two。 Default method

In fact, there is a built-in way to "update" the dictionary D1 with another dictionary D2.

Dnew = d1.copy () dnew.update (D2)

One drawback is that the .update () method modifies the dictionary in place. You need to copy D1 to create a new dictionary dnew. This "built-in" approach cannot easily merge dictionaries.

Can you merge it into one line of code? Sure!

3. A "neat" approach

Python supports dictionary decompression from version 3.5 +. You can create a new merge dictionary by unpacking the elements in the two dictionaries.

Dnew = {* * D1, * * d2}

This decompression method has become the practical method used in Python3.5+ merge dictionary. However, this grammar may not be beautiful to some people, and it is obviously not intuitive to most people. Can you guess what it means when you see it for the first time?

There is another concise way to merge dictionaries with a single line of code. It doesn't look intuitive either.

Dnew = dict (D1, * * D2)

4. Clean methods in Python 3.9

Python 3.9 introduces a new clean (!) Method, using the union operator "|" to merge dictionaries. Very concise.

Dnew = D1 | dumped dnew = = {'name':' Tom', 'age': 20,' gpa': 4.0, 'is_single': True}

This union operator is actually not new in Python. It can be used to "merge" two collections. Collections are unordered and unindexed collections, also enclosed in curly braces.

A = {1pr 2,3} b = {3,4,5} print (a | b) # {1Jing 2,3,4,5}

Extended assignment

For two lists or values an and b, a + = b is an abbreviation for a = a + b. This extended assignment behavior also applies to dictionary union operators. This means that D1 | = D2 equals D1 = D1 | D2.

Thank you for your reading, the above is the content of "what are the methods of merging dictionaries in Python". After the study of this article, I believe you have a deeper understanding of what the method of merging dictionaries in Python has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report