Posts

Showing posts with the label update

Concatenate Two Dictionaries | Python How To?

If you want to concatenate two dictionaries in Python (add a dictionary to another dictionary), there is a simple way to do it. Use the update() method which is much like the extend() function used to concatenate lists. >>> dict_1 = {1: 'a', 2: 'b', 3: 'c'} >>> dict_2 = {4: 'd', 5: 'e', 6: 'f'} >>> dict_1 {1: 'a', 2: 'b', 3: 'c'} >>> dict_2 {4: 'd', 5: 'e', 6: 'f'} >>> dict_1.update(dict_2) >>> dict_2 {4: 'd', 5: 'e', 6: 'f'} >>> dict_1 {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'} >>> What is your other tricks for concatenating Python dictionaries?