Sitemap
Chinthapooja

Chinthapooja

Python Tips: Merging dictionaries Using Pipe ‘|’ Operator

2 min readAug 31, 2024
Press enter or click to view image in full size
Merge dictionaries using pipe '| ‘ operator provides a concise and readable way to merge dictionaries, enhancing code clarity

In this blog post, we’ll explore how to merge two dictionaries using the | operator.

In Python, the pipe operator (|) was traditionally used for bitwise operations, particularly bitwise OR. However, starting with Python 3.9, its functionality was extended to include dictionary merging.

dct1 = {'a':1,'b':2,'c':3}
dct2 = {'a':10,'y':25,'z':26}
new_dct = dct1 | dct2
#output: {'a': 10, 'b': 2, 'c': 3, 'y': 25, 'z': 26}

Here, both dct1 and dct2 contain key-value pairs. dct1 includes the keys 'a', 'b', and 'c', and dct2 includes the keys 'a', 'y', and 'z'.

If a key exists in both dictionaries i.e, both dct1 and dct2 contain the key 'a' , the value from the second dictionary (dct2) overrides the value from the first dictionary (dct1).

Get Chinthapooja’s stories in your inbox

Join Medium for free to get updates from this writer.

Merging dictionaries can also be done using the update() and dictionary unpacking.

# Using update()
dct1 = {'a': 1, 'b': 2, 'c': 3}
dct2 = {'a': 10, 'y': 25, 'z': 26}
new_dct = dct1.copy()
new_dct.update(dct2)
#output: {'a': 10, 'b': 2, 'c': 3, 'y': 25, 'z': 26}

# Using Dictionary Unpacking
dct1 = {'a': 1, 'b': 2, 'c': 3}
dct2 = {'a': 10, 'y': 25, 'z': 26}
new_dct = {**dct1, **dct2}
#output: {'a': 10, 'b': 2, 'c': 3, 'y': 25, 'z': 26}

Advantages of using the Pipe ‘|’ operator over other methods:

  • The | operator is a more Pythonic way of handling dictionary merges, aligning with Python’s philosophy of readability and simplicity. It’s intuitive and aligns with how other operators work in Python, such as + for string or list concatenation.
  • Unlike the update() method, which modifies the original dictionary, the | operator returns a new dictionary, leaving the original dictionaries unchanged.
  • The | operator provides a clean and straightforward syntax for merging dictionaries. It makes our code easier to read and understand, especially when compared to older methods like using update() or dictionary unpacking.

Applications:

  • When aggregating data from multiple sources, such as combining data from different APIs, databases, or sensors, the pipe operator can help merge dictionaries representing different datasets.
  • When working with databases, one might need to build dynamic queries based on user input or other runtime conditions. The pipe operator can be used to combine base query parameters with additional filters.
base_query = {'select': '*', 'from': 'users'}
filter_by_age = {'where': 'age > 18'}

final_query = base_query | filter_by_age
# output: {'select': '*', 'from': 'users', 'where': 'age > 18'}
  • In frameworks or libraries where default behavior is defined using dictionaries, developers can use the pipe operator to override defaults with custom behavior.
default_options = {'timeout': 5, 'retry': 3, 'cache': True}
custom_options = {'timeout': 10, 'cache': False}

final_options = default_options | custom_options
# output: {'timeout': 10, 'retry': 3, 'cache': False}

--

--