The |= in-place OR operator in Python

Borislav Hadzhiev

Last updated: Sep 21, 2022

Photo from Unsplash

- The |= in-place OR operator with Set objects
- The |= in-place OR operator with Dictionaries
- The |= in-place OR operator with Numbers

**When used with set objects, the in-place |= OR operator is used to reassign
a set to the union of 2 sets, e.g. set_1 |= set_2. The |= operator is
equivalent to set_1 = set_1 | set_2.**

main.py

`set_1 = {'bobby'} set_2 = {'hadz'} # 👇️ reassign my_set to the union of my_set and my_set_2 set_1 |= set_2 print(set_1) # 👉️ {'bobby', 'hadz'}`

The union of two

`set`

objects is the smallest `set`

which contains the elements of both `set`

objects.The `a |= b`

operator is a shorthand for `a = a | b`

.

main.py

`set_1 = {'bobby'} set_2 = {'hadz'} set_1 = set_1 | set_2 print(set_1) # 👉️ {'bobby', 'hadz'}`

In both cases, we reassign the `set_1`

variable to the union of `set_1`

and
`set_2`

.

However, you can use the OR (|) operator without an in-place reassignment.

main.py

`set_1 = {'bobby'} set_2 = {'hadz'} print(set_1 | set_2) # 👉️ {'bobby', 'hadz'}`

Whereas you can only use the in-place OR (|=) operator with in-place reassignment.

main.py

`set_1 = {'bobby'} set_2 = {'hadz'} set_1 |= set_2 print(set_1) # 👉️ {'bobby', 'hadz'}`

You can also use the in-place OR (|=) and the OR (|) operators in the same statement.

main.py

`set_1 = {'bobby'} set_2 = {'hadz'} set_3 = {'com'} set_1 |= set_2 | set_3 print(set_1) # 👉️ {'com', 'bobby', 'hadz'} print(set_2) # 👉️ {'hadz'} print(set_3) # 👉️ {'com'}`

The `set_1`

variable is reassigned to the union of the 3 `set`

objects.

The other 2 `set`

objects remain unchanged.

**When used with two dictionaries, the in-place |= OR operator reassigns the
variable by merging the two dictionaries. If a key appears in both dictionaries,
the value from the right-hand operand wins.**

main.py

`dict_1 = {'a': 1, 'b': 2} dict_2 = {'a': 10, 'c': 30} # 👇️ reassign dict_1 by merging it with dict_2 dict_1 |= dict_2 print(dict_1) # 👉️ {'a': 10, 'b': 2, 'c': 30}`

Notice that the key `a`

is present in both dictionaries, so the value of the key
in the right operand wins.

The `dict_1 |= dict_2`

statement is a shorthand for `dict_1 = dict_1 | dict_2`

.

main.py

`dict_1 = {'a': 1, 'b': 2} dict_2 = {'a': 10, 'c': 30} dict_1 = dict_1 | dict_2 print(dict_1) # 👉️ {'a': 10, 'b': 2, 'c': 30}`

The `|`

operator is called the
dictionary merge
operator and the `|=`

operator is called the dictionary update operator.

The dictionary merge `|`

operator can be used without reassigning the variable.

main.py

`dict_1 = {'a': 1, 'b': 2} dict_2 = {'a': 10, 'c': 30} print(dict_1 | dict_2) # 👉️ {'a': 10, 'b': 2, 'c': 30}`

Whereas the dictionary update `|=`

operator can only be used with in-place
reassignment.

main.py

`dict_1 = {'a': 1, 'b': 2} dict_2 = {'a': 10, 'c': 30} dict_1 |= dict_2 print(dict_1) # 👉️ {'a': 10, 'b': 2, 'c': 30}`

The `|`

and `|=`

operators were added in Python 3.9. Here is a link to the
official proposal.

You can also use the dictionary update `|`

and the dictionary merge `|=`

operators in a single statement.

main.py

`dict_1 = {'a': 1, 'b': 2} dict_2 = {'a': 10, 'c': 30} dict_3 = {'a': 100, 'd': 40, 'e': 50} dict_1 |= dict_2 | dict_3 print(dict_1) # 👉️ {'a': 100, 'b': 2, 'c': 30, 'd': 40, 'e': 50}`

Key conflicts are resolved by keeping the rightmost value.

**When used with numbers, the in-place |= bitwise OR operator is used to do
binary math. The statement num1 =| num2 is equivalent to
num1 = num1 | num2.**

main.py

`num_1 = 3 # 👉️ (binary = 011) num_2 = 4 # 👉️ (binary = 100) num_1 |= num_2 print(num_1) # 👉 7️ (binary = 111)`

The bitwise OR

`|`

operator does a "bitwise OR". Each bit of the output is `0`

if the corresponding bit of `X`

and `Y`

is `0`

, otherwise it is `1`

.The `|=`

operator reassigns the value of the variable in place.

You can also use the long-form syntax to achieve the same result.

main.py

`num_1 = 3 # 👉️ (binary = 011) num_2 = 4 # 👉️ (binary = 100) num_1 = num_1 | num_2 print(num_1) # 👉 7️ (binary = 111)`

Each bit in the result is set if the corresponding bit is set in either of the
two numbers, otherwise the bit is `0`

.