The update operator ( | =) of Python dictionary class

The update operator ( | =) of Python dictionary class updates the contents of the dictionary d1 from the contents of d2 or an iterable containing name value pairs.

Dictionary Updated 20 Nov 2020 1 min read

Operator Name

The update operator ( |= )

Overview

The update operator ( |= ) updates the contents of the dictionary instance(known as the current object, self, this and in other names) with the contents from an another dictionary instance d2 or an iterable contaning itreables of name-value pairs.

Operands

d1 - The current dictionary
d2 - The dictionary from which d1 needs to be updated

Return Value

A dict instance containing the updated name-value pairs.

Example

python
# Example Python program that updates a dictionary

            # from another mapping

            original = {"S1":62.5,

                        "S2":110.5,

                        "S3":125.1,

                        "S4":220.8,

                        "S5":37.5,

                        };

reconciled = {"S1":62.5,

                          "S2":110.5,

                          "S3":125.1,

                          "S4":220.8,

                          "S5":37.5,

                          "S6":293.1,

                        };

original.update(reconciled);

            print(reconciled);

Output

Output
{'S1': 62.5, 'S2': 110.5, 'S3': 125.1, 'S4': 220.8, 'S5': 37.5, 'S6': 293.1}