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?
原文地址:Concatenate Two Dictionaries | Python How To?, 感谢原作者分享。 生命有限,努力无限