Python implements the merge of two dictionaries (dict), pythondict

Source: Internet
Author: User

Python implements the merge of two dictionaries (dict), pythondict

This article describes how to merge two dictionaries (dict) in Python. The specific method is as follows:

Two existing dictionaries, dict, are as follows:

dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}

Merge the two dictionaries to get a similar result:

{1:[1,11,111],2:[2,22,222],3:[3,33,333],4:[4,44,444]}

Method 1:

dictMerged1=dict(dict1.items()+dict2.items())

Method 2:

dictMerged2=dict(dict1, **dict2)

Method 2 is equivalent:

dictMerged=dict1.copy()dictMerged.update(dict2)

Or:

dictMerged=dict(dict1)dictMerged.update(dict2)

Method 2 is much faster than method 1. The timeit test is as follows:

$ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged1=dict(dict1.items()+dict2.items())'  10000 loops, best of 3: 20.7 usec per loop$ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged2=dict(dict1,**dict2)'  100000 loops, best of 3: 6.94 usec per loop$ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged3=dict(dict1)' 'dictMerged3.update(dict2)'  100000 loops, best of 3: 7.09 usec per loop$ python -m timeit -s 'dict1=dict2=dict((i,i) for i in range(100))' 'dictMerged4=dict1.copy()' 'dictMerged4.update(dict2)'  100000 loops, best of 3: 6.73 usec per loop

I hope this article will help you with Python programming.


Combine the two dictionaries in python

Dict1 = {'1': ('A', 'B', 'C', 'D'), '2': ('F', 'w ', 'M'), '3' :( 'P', 'l', 'w ')}
Dict2 = {'1': ('B', 'w', 'q'), '2' :( 'I', 'z', 'M '), '3' :( 'P', 'w', 'O ')}

Dict = {}
For key in dict1.keys ():
Dict [key] = list (dict1 [key])

For key in dict2.keys ():
If dict. has_key (key ):
# Append
For v in dict2 [key]:
If not v in dict [key]:
Dict [key]. append (v)
Else:
Dict [key] = list (dict2 [key])

Print dict

Python combines two dictionaries into one.

Dict1 = {'1': ('A', 'B', 'C', 'D'), '2': ('F', 'w ', you probably haven't run the content in python at all, have you? Let's see the two dictionaries I have changed.

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.