A comparison of common methods and efficiencies in Python merging two dictionaries

Source: Internet
Author: User
Tags comparison in python

This article illustrates the common method of Python merging two dictionaries with efficiency comparisons. Share to everyone for your reference. The specific analysis is as follows:

The following code illustrates 5 methods of merging two dictionaries and makes a simple performance test

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

In addition to the F4 method that can cause destructive modifications to the dictionary D1, several other methods return the merged results as a new dictionary.

Here is the test result:

?

1 2 3 4 5 6 7 F1 cost:2.382999897 F2 cost:4.45399999619 f3 cost:3.02100014687 f4 cost:1.73000001907 f5 cost:2.3710000515 f6 cost:2.8970 0007439 F7 cost:2.35600018501

It can be seen that F4 is the most efficient, and the F4 method is recommended if you do not need to keep the original dictionary.

I hope this article will help you with your Python programming.

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.