#Day27 — Fastest Way to Combine Dictionaries
Published in
3 min readApr 17, 2021
We know the fastest way to combine lists. Today we will try to find the fastest way to combine dictionaries
We will be considering the following ways
- Using a for loop
- Using the update() method
- Using the ChainMap Function
- Using the unpacking operator
- Using the pipe operator ( | )
- Using the (|=) operator
How are we going to measure the performance?
We have a couple of dictionaries with 1000 key-value pairs in each dictionary. We will update function func to perform the different combination methods. The function will be called 100 times and we will print the average run time.
Using a for loop
We simply iterate over the second dictionary and add its elements to the first dictionary.
Below is the output