Get the App
SLTechnology News&Howtos  ›  Development  › 

How to merge dictionaries with double asterisk syntax in python

Shulou Source: shulou.com Published: 2022-06-01 05:15:34 09月30日 Update

This article mainly introduces how python uses double star syntax to merge dictionaries, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Merging dictionaries with double-star syntax ** (Python 3.5+)

This syntax is new since Python 3.5. If we have two dictionaries and want to merge them, we can use curly braces and double marks for both dictionaries. Here dictionary 1 has names and ages, dictionary 2 also has names and cities. After merging with this concise syntax, our final dictionary has all three keys.

d1 = {'name':'Alex', 'age': 25} d2 = {'name': 'Alex', 'city': 'New York'} merged_dict = {**d1, **d2} print (merged_dict) # {'name': 'Alex', 'age': 25, 'city': 'New York'} Thank you for reading this article carefully. I hope that Xiaobian's shared article "Python how to merge dictionaries with double star syntax" will be helpful to everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Tags: Dictionary grammar double star article two name brevity value interest at the same time city age parenthesis more friends knowledge editing industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Apple vpn NVidia