Combine multiple dictionaries and add multiple elements at once, safely and non-destructively in Python

Thing you want to do

--I want to combine multiple dictionaries into one dictionary ――I want to add other elements as well

dictA = {'A1': 1, 'A2': 1}
dictB = {'B1': 1, 'B2': 1}
# ---(Combine dictionaries&Add element)---
# => {'key1': 1, 'key2': 1, 'A1': 1, 'A2': 1, 'B1': 1, 'B2': 1}

further,

--I want to process it non-destructively (without making any changes to the original dictionary) --Do not change the value (I want an error if the key is duplicated)

I would like to introduce an efficient writing method to realize these.

Bad writing

This is partly possible with ``` .update ()` `` and additional dictionary notation.

dictA.update(dictB)
dictA['key1'] = 1
dictA['key2'] = 1
new_dict = dictA 
print(new_dict)
# => {'A1': 1, 'A2': 1, 'B1': 1, 'B2': 1, 'key1': 1, 'key2': 1}

Demerit

The original dictA is gone. (Because of adding elements (destructive changes) to dictA)

print(dictA)
# => {'A1': 1, 'A2': 1, 'B1': 1, 'B2': 1, 'key1': 1, 'key2': 1}

Also, if the key is duplicated, the value will be overwritten. This can cause unintended behavior (causing bugs).

dictA = {'key': 'dictA'}
dictB = {'key': 'dictB'}
dictA.update(dictB)
print(dictA)
# => {'key': 'dictB'}

Proper writing

Create a new dictionary by passing values to the keyword arguments of the dict function without worrying about joining or adding elements. Any number is possible.

new_dict = dict(
    key1=1,
    key2=1,
    **dictA,
    **dictB,
)
print(new_dict)
# => {'key1': 1, 'key2': 1, 'A1': 1, 'A2': 1, 'B1': 1, 'B2': 1}

Non-destructive

The original dictionary is unchanged because it is a non-destructive process.

new_dict['keyA1'] = 5

print(new_dict['keyA1'])
# => 5
print(dictA['keyA1'])
# => 1

Behavior when there is duplication

If there are duplicates, a TypeError will occur, so you can avoid overwriting unintended values. An error will occur for duplicate keys in the dictionary and all explicitly written keyword arguments.

dictA = {'key': 'dictA'}
dictB = {'key': 'dictB'}

new_dict = dict(
    key='A',
    **dictA,
    **dictB,
)
# => TypeError: type object got multiple values for keyword argument 'key'

Summary

It was said that if you recreate it with the dict function, you can combine dictionaries and add elements safely and concisely!

Recommended Posts

Combine multiple dictionaries and add multiple elements at once, safely and non-destructively in Python
Delete multiple elements in python list
Use gitlabApi to add members to multiple groups in gitlab at once
[Python] Combine all the elements in the array
Convert multiple proto files at once with python
Register multiple self-made styles in Word at once
[Python] Manipulation of elements in list (array) [Add / Delete]
How to swap elements in an array in Python, and how to reverse an array.
[Linux] Grep multiple gzip files in a directory at once
Sort and output the elements in the list as elements and multiples in Python.
Multiple regression expressions in Python
Rsync multiple files at once
Stack and Queue in Python
Avoid multiple loops in Python
Unittest and CI in Python
Getting list elements in Python
Prohibit multiple launches in python
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
Pass the selected item in Tablacus Explorer from JScript to python and rename it all at once
[Python] How to save the installed package and install it in a new environment at once Mac environment