Python |= In-Place OR Operator

Have you stumbled upon the strange-looking Python operator A |= B in a code snippet and you don’t know what it means? This article will clarify it once and for all! Let’s start with the short answer:

Python’s A |= B applies the | operator in place. Thus, it is semantically identical to the longer-form version A = A | B of first performing the operation A | B and then assigning the result to the variable A.

The following minimal example creates two Boolean variables A and B and performs the in-place B |= A operation to perform a logical OR operation B | A and assigning the result to the first operand B that becomes True:

>>> A = True
>>> B = False
>>> B |= A
>>> B
True

In this example, you’ve seen this in-place operation on Boolean operands. But the | operator is overloaded in Python. The three most frequent use cases for the | and |= operators are the following:

Let’s dive into each of them one by one.

|= on Python Sets

Python’s set.union(set_1, set_2, ...) creates and returns a new set consisting of the elements that are members of any of the involved sets. A shorthand notation for the set union operator is the | operator such as in set_1 | set_2 | set_3. The set_1 |= set_2 operator performs the set union operator in-place—it replaces the set given as a first operand.

For example, the following three expressions are semantically equivalent—they all perform the union of sets set_1 and set_2 and assign the result to the set_1 variable.

>>> set_1 = set_1 | set_2
>>> set_1 |= set_2
>>> set_1.__ior__(set_2)

The first operation is an assigned OR operation. The second operation is an in-place OR. The third is an in-place operation using a special “dunder” method.

The following minimal example shows how set_1 is updated with the union of the two sets, in-place:

>>> set_1 = {'Alice'}
>>> set_2 = {'Bob', 'Alice', 1, 2, 3}
>>> set_1 |= set_2
>>> set_1
{1, 2, 3, 'Bob', 'Alice'}

|= on Dictionaries

Python 3.9 has introduced the merge and update operators on dictionaries.

  • dict_1 | dict_2 creates a new dictionary with all elements in dict_1 and dict_2. The second operand takes precedence over the first, so if a key exists in both dictionaries, Python uses the (key, value) pair from the second dictionary.
  • dict_1 |= dict_2 updates the first dictionary dict_1 with the same merged dictionary elements.

In the following example, we updated the first dictionary with the (key, value) pairs from the second dictionary:

d1 = {'Alice': 42, 'Bob': 18}
d2 = {'Alice': 18, 'Carl': 22}
d1 |= d2
print(d1)

The output is the updated dictionary

{'Alice': 18, 'Bob': 18, 'Carl': 22}

|= on Booleans

The Python |= operator when applied to two Boolean values A and B performs the logical OR operation A | B and assigns the result to the first operand A. As a result, operand A is False if both A and B are False and True otherwise.

This is shown in the following example where variable B is updated with the result of the operation B | A using the in-place Boolean OR operation B |= A.

>>> A = True
>>> B = False
>>> B |= A
>>> B
True

Python In-Place Operators

In-place assignment operators (also called compound assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, x += 3 is the same as x = x + 3 of first calculating the result of x +3 and then assigning it to the variable x.

OperatorNameShort ExampleEquivalent Long Example
=In-place Assignmentx = 3
+=In-place Additionx += 3x = x + 3
-=In-place Subtractionx -= 3x = x - 3
*=In-place Multiplicationx *= 3x = x * 3
/=In-place Divisionx /= 3x = x / 3
%=In-place Modulox %= 3x = x % 3
//=In-place Integer Divisionx //= 3x = x // 3
**=In-place Powerx **= 3x = x ** 3
&=In-place Bitwise Andx &= 3x = x & 3
|=In-place Bitwise Orx |= 3x = x | 3
^=In-place Bitwise XORx ^= 3x = x ^ 3
>>=In-place Bitwise Shift Rightx >>= 3x = x >> 3
<<=In-place Bitwise Shift Leftx <<= 5x = x << 5

Summary

Python’s A |= B applies the | operator in place. Thus, it is semantically identical to the longer-form version A = A | B of first performing the operation A | B and then assigning the result to the variable A.

The | operator is most often used as one of the following:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!