Master Python Dictionaries: Syntax, Methods, Updates, Sorting & Merging
What Is a Dictionary in Python?
A Python dictionary is a mutable, unordered collection of key‑value pairs. Keys must be immutable (e.g., strings, numbers, tuples), while values can be any data type. Dictionaries are declared with curly braces:
students = {"Tim": 18, "Charlie": 12, "Tiffany": 22, "Robert": 25}
Python 3.7+ preserves insertion order, but the core concept remains the same: each key maps uniquely to a value.
Key Properties
- Keys are unique; duplicate keys overwrite previous entries.
- Keys must be immutable (strings, numbers, tuples).
- Key comparison is case‑sensitive.
Dictionary Syntax
Dict = { 'Tim': 18, 'xyz': 25 }
Keys and values are separated by colons; pairs are comma‑delimited.

Common Dictionary Methods
copy()
Creates a shallow copy of the dictionary.
original = {'Tim': 18, 'Charlie': 12}
copy_dict = original.copy()
update()
Updates the dictionary with key‑value pairs from another mapping or iterable.
students.update({'Sarah': 9})
items()
Returns a view of the dictionary’s key‑value pairs.
list(students.items())
len()
Returns the number of key‑value pairs.
len(students)
Membership Test (in)
Checks if a key exists in the dictionary.
"email" in my_dict
Updating and Deleting Entries
Add or Modify a Key
students.update({"Sarah": 9})
Delete a Key
del students["Charlie"]
Iterating and Sorting
While dictionaries are unordered, you can sort keys for display.
for name in sorted(students):
print(f"{name}:{students[name]}")
Conversion to String
Use str() to get a printable representation.
print(str(students))
Merge Two Dictionaries
Two popular methods:
- Using
update():
dict1.update(dict2)
** unpacking operator (Python 3.5+):merged = {**dict1, **dict2}
Sample Code Snippets
Python 3 Example – Create and Print a Dictionary
students = {"Tim": 18, "Charlie": 12, "Tiffany": 22, "Robert": 25}
print(students)
Check Key Existence
if "Tim" in students:
print("Tim is present")
Method Reference Table
| Method | Description | Syntax |
|---|---|---|
| copy() | Shallow copy of the dictionary | dict.copy() |
| update() | Merge or add key‑value pairs | dict.update(other) |
| items() | View of key‑value tuples | dict.items() |
| len() | Number of entries | len(dict) |
| in | Check key existence | key in dict |
Key Takeaways
- Dictionary keys are unique, immutable, and case‑sensitive.
- Values can be any data type, including nested dictionaries or lists.
- Use
copy(),update(), and**for flexible data manipulation. - Membership tests with
inare fast and Pythonic. - Python 3.7+ guarantees insertion order, simplifying iteration.
Python
- Mastering Python Dictionaries: Creation, Manipulation, and Advanced Techniques
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Python len(): A Practical Guide to Measuring Object Lengths
- Python Queue: Understanding FIFO & LIFO with Practical Examples
- Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
- Copy Files in Python with shutil.copy() and shutil.copystat()
- Creating ZIP Archives in Python: From Full Directory to Custom File Selection
- Python List index() – How to Find Element Positions with Practical Examples
- Python Calendar Module: Expert Guide with Code Examples
- Python Dictionary Fundamentals: Keys, Values, and Access