Skip to content

Archive

Dictionary

2 articles
Python Updated 02 Sep 2025 2 min read

Merging Multiple Dictionaries in Python

Combining dictionaries is common when assembling configuration, request data, defaults, or results from multiple sources. Python provides several approaches, and the right one depends on the Python version and whether you want to mutate an existing dictionary. 1. Dictionary Union with | (Python 3.9+) Modern Python supports the dictionary union operator: dict1 = {"a": 1, "b": 2} dict2 = {"b": 3, "c": 4} dict3 = {"d": 5} merged = dict1 | dict2 | dict3 print(merged) Output:

Python Updated 02 Sep 2025 2 min read

Check Whether a Key Exists in a Python Dictionary

Python dictionaries provide fast key-based lookup, and checking whether a key exists is a common operation. Use the in Operator The clearest and most idiomatic solution is: my_dict = {"name": "Alice", "age": 30, "city": "New York"} if "name" in my_dict: print("The key 'name' exists.") else: print("The key 'name' does not exist.") Membership tests on a dictionary check keys by default.