Understanding dictionaries in Python 3 is one of the most essential and powerful skills in Python programming — dictionaries are mutable, unordered (pre-3.7) or insertion-ordered (3.7+) collections of key-value pairs that let you store and retrieve data by meaningful names (keys) instead of numeric indices, making them perfect for user profiles, configurations, JSON-like data, caching, counting, grouping, and almost every real-world Python application.

In this up-to-date 2025–2026 guide, you’ll master exactly how dictionaries in Python 3 work: creating dictionaries, accessing/adding/updating/deleting items, looping over keys/values/items, merging dictionaries, dictionary comprehensions, common use cases, and best practices. All examples are tested on Python 3.10–3.13.

Key Takeaways – Dictionaries in Python 3

  • Dictionaries in Python 3 are mutable mappings of unique, immutable keys to any values, created with {} or dict().
  • Keys must be immutable (strings, numbers, tuples); values can be anything (lists, dicts, objects).
  • Access values with dict[key] — raises KeyError if missing; use .get() for safe access.
  • Since Python 3.7, dictionaries maintain insertion order — very useful for predictable iteration.
  • Add/update with dict[key] = value or .update().
  • Delete with del dict[key] or .pop(key).
  • Loop efficiently with .keys(), .values(), .items() — or just for k in dict:.
  • Dictionary comprehensions: {k: v*2 for k, v in old_dict.items()} — concise creation.
  • Merge dictionaries with operator (Python 3.9+), dict.update(), or unpacking {**d1, **d2}.

Prerequisites

  • Python 3.8+ installed
  • Basic Python knowledge (lists, indexing, loops)
  • Interactive shell (python3) or script file

1. Creating Dictionaries in Python 3

				
					# Basic literal syntax
user = {
    'username': 'zain_khi',
    'age': 30,
    'city': 'Karachi',
    'is_active': True,
    'scores': [95, 88, 92]
}
print(user)

# Using dict() constructor
config = dict(
    theme='dark',
    debug=True,
    api_key='abc123'
)
print(config)
				
			

2. Accessing Dictionary Items

				
					print(user['username'])     # zain_khi

# Safe access (no KeyError)
print(user.get('email', 'Not found'))  # Not found

# .get() with default
level = user.get('level', 1)
print(level)                # 1 (default used)
				
			

3. Adding & Updating Dictionary Items

				
					# Add new key-value
user['email'] = 'zain@example.com'

# Update existing
user['age'] = 31

# Multiple updates with .update()
user.update({
    'last_login': '2026-02-08',
    'is_premium': True
})
print(user)
				
			

4. Deleting & Removing Items

				
					# Delete by key
del user['scores']

# Remove & return value
last_login = user.pop('last_login', 'Never')
print(last_login)           # 2026-02-08

# Clear entire dictionary
user.clear()
print(user)                 # {}
				
			

5. Looping Over Dictionaries in Python 3

				
					user = {'name': 'Zain', 'age': 30, 'city': 'Karachi'}

# Keys only (default)
for key in user:
    print(key)              # name, age, city

# Values
for value in user.values():
    print(value)

# Key-value pairs (most common)
for key, value in user.items():
    print(f"{key}: {value}")
				
			

6. Dictionary Comprehensions

				
					# Basic
squares = {x: x**2 for x in range(6)}
print(squares)  # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

# With condition
even_squares = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares)  # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
				
			

7. Merging Dictionaries (Python 3.9+ | Operator)

				
					defaults = {'theme': 'dark', 'debug': False}
user_settings = {'theme': 'light', 'notifications': True}

# Merge with |
merged = defaults | user_settings
print(merged)  # {'theme': 'light', 'debug': False, 'notifications': True}

# Older ways (still valid)
merged2 = {**defaults, **user_settings}
defaults.update(user_settings)
				
			

8. Best Practices & Modern Tips (2025–2026)

  • Use meaningful keys: ‘user_id’ > ‘uid’
  • Prefer .get() over direct access when key may be missing.
  • Use dict comprehensions for clean transformations.
  • Type hints: user: dict[str, any] = {…}
  • For ordered dicts before 3.7 → use collections.OrderedDict
  • Avoid mutable defaults: def func(d: dict = None): d = d or {}

How to Use Dictionaries in Python 3 – FAQ (2025–2026)

  1. How do I create dictionaries in Python 3?
    Use {key: value} or dict(key=value) — basic way to start with dictionaries in Python 3.
  2. How do I access dictionary values safely in Python 3?
    dict.get(‘key’, default) — prevents KeyError.
  3. How do I merge dictionaries in Python 3?
    d1 | d2 (3.9+), {**d1, **d2}, or d1.update(d2).
  4. Can I use lists as dictionary keys in Python 3?
    No — keys must be immutable (use tuples instead).
  5. What’s the difference between dict.keys() and looping over dict?
    for k in dict: is shorthand for for k in dict.keys() — both common.

Summary

You now know exactly how dictionaries in Python 3 work: creating, accessing/updating/deleting items, looping over keys/values/items, merging, dictionary comprehensions, and modern best practices.

Mastering dictionaries in Python 3 unlocks powerful data organisation — user profiles, configurations, counting, grouping, caching, JSON handling, and more. Dictionaries are one of Python’s most versatile and commonly used data structures.

Recommended Next Tutorials

  • Python Dictionary Comprehensions & Advanced Patterns
  • Python Sets – Unique Collections
  • Python Collections Module (defaultdict, Counter, OrderedDict)
  • Build a User Profile / Config Manager with Dictionaries
  • Python Data Structures Cheat Sheet