β

Concatenate Two Dictionaries | Python How To?

life is short - you need Python! 196 阅读
If you want to concatenate two dictionaries in Python (add a dictionary to another dictionary), there is a simple way to do it. Use the update() method which is much like the extend() function used to concatenate lists.

>>> dict_1 = {1: 'a', 2: 'b', 3: 'c'}
>>> dict_2 = {4: 'd', 5: 'e', 6: 'f'}
>>> dict_1
{1: 'a', 2: 'b', 3: 'c'}
>>> dict_2
{4: 'd', 5: 'e', 6: 'f'}
>>> dict_1.update(dict_2)
>>> dict_2
{4: 'd', 5: 'e', 6: 'f'}
>>> dict_1
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'}
>>>

What is your other tricks for concatenating Python dictionaries?
作者:life is short - you need Python!
A blog on Python with tutorials, code, programs, tips and tricks, how-to, book-list, crawler / spide
原文地址:Concatenate Two Dictionaries | Python How To?, 感谢原作者分享。

发表评论