# Different ways for merging dictionary and performance comparison

Posted by Afsal on 06-May-2022

Hi Pythonistas

Today we are going to analyze the speed comparison of different ways of merging dict. Let us see with examples

``````a = {str(i): i for i in range(100)}

b = {str(i): i for i in range(101, 200)}``````

Using update method of dict

``````def using_update():
c = a.copy()
c.update(b)
return  c``````

Using merge operator

``````def using_merge_operator():
return a | b``````

Using unpacking

``````def using_unpacking():
return { **a, **b }``````

Using chain map

``````from collections import ChainMap

def using_chainmap():
return ChainMap(a, b)``````

Using itertools chain operator

``````def using_chain():
return itertools.chain(a.items(), b.items())``````

Analysis using timeit module

``````time_taken = timeit.timeit(using_update, number=1000_000)
print("Total time for using_update", time_taken)

time_taken = timeit.timeit(using_merge_operator, number=1000_000)
print("Total time for using merge", time_taken)

time_taken = timeit.timeit(using_unpacking, number=1000_000)
print("Total time for using unpacking", time_taken)

time_taken = timeit.timeit(using_chainmap, number=1000_000)
print("Total time for using chainmap", time_taken)

time_taken = timeit.timeit(using_chain, number=1000_000)
print("Total time for using chain", time_taken)``````

Output

``````Total time for using_update 2.3451301979985146

Total time for using merge 2.228314269999828

Total time for using unpacking 2.357389224001963

Total time for using chainmap 0.24351109200142673

Total time for using chain 0.17889608000041335``````

We can see that the chain method and chaimap are much faster compared to dictionary operations. Issue with chain operator it returns an iterable we cannot access using the key.

But in the case of chainmap it takes less time and can be accessed using a key.

Hope you have learned something from this post. Please share your suggestions to afsal@parseltongue.co.in
{"ft_lang_label":"__label__en","ft_lang_prob":0.5651465,"math_prob":0.9808949,"size":1780,"snap":"2023-40-2023-50","text_gpt3_token_len":454,"char_repetition_ratio":0.19594595,"word_repetition_ratio":0.034632035,"special_character_ratio":0.29213482,"punctuation_ratio":0.14006515,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9775932,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-01T06:15:55Z\",\"WARC-Record-ID\":\"<urn:uuid:3e5721ea-6b74-4589-b956-9f10ce40ee58>\",\"Content-Length\":\"12327\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ce37249b-5b51-40a7-b230-5db06a697f8d>\",\"WARC-Concurrent-To\":\"<urn:uuid:4241b1b4-818e-4649-973d-cc0e9dcd7e90>\",\"WARC-IP-Address\":\"3.6.242.200\",\"WARC-Target-URI\":\"https://parseltongue.co.in/different-ways-for-merging-dictionary-and-performance-comparison/\",\"WARC-Payload-Digest\":\"sha1:F7LDJZGAQGDWQ62CJFON7Z3ZSYWNL457\",\"WARC-Block-Digest\":\"sha1:FSCVUDOEAB3YFPSTYUZNCJ4YNQ6VFPSP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510781.66_warc_CC-MAIN-20231001041719-20231001071719-00844.warc.gz\"}"}