Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →A Python dictionary stores values under unique keys: use a key to look up the value it identifies. Create one with braces, retrieve entries with square brackets, and use get() when a key might be missing. This guide covers the everyday patterns, safe lookups, iteration, and the rules for choosing keys.
What is a Python dictionary?
A dictionary, written dict in Python, is a mutable mapping of keys to values. Each key identifies its associated value, so you look up a dictionary entry by key rather than by numeric position.
A list is a sequence: its elements are accessed by position, such as items[0]. A dictionary instead associates unique keys with values, such as person["name"]. Both can be useful in the same program, but they represent different kinds of access. See the Python Tutorial: Data Structures.
How do I create a dictionary?
Use {} for an empty dictionary, or place comma-separated key: value pairs inside braces:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problems#1 Best Overall
person = {"name": "Ada", "age": 36}
empty = {}
You can also call dict() to make an empty dictionary or construct one from an iterable of key/value pairs:
scores = dict([("Mina", 92), ("Leo", 85)])
Each key appears at most once in a dictionary. If a key is repeated in a literal, its later value replaces the earlier value:
settings = {"theme": "light", "theme": "dark"}
print(settings["theme"]) # dark
How do I access and change dictionary values?
Use square brackets with a key to retrieve its value. Assign to an existing key to replace its value, or assign to a new key to add an entry:
Rank #2
person = {"name": "Ada", "age": 36}
print(person["name"]) # Ada
person["age"] = 37 # replace a value
person["city"] = "London" # add an entry
To remove an entry, use del with the key:
del person["city"]
How do I avoid a KeyError?
Indexing with a key that is not in the dictionary raises KeyError. For optional data, get() returns None when the key is absent, or a fallback value you provide:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
email = person.get("email")
contact = person.get("email", "not provided")
Use in when you need to test whether a key exists before acting on it:
if "name" in person:
print(person["name"])
These approaches answer different questions: get() retrieves a value with a fallback, while in checks key membership.
What can be a dictionary key?
Keys must be hashable so Python can find them reliably. Strings and numbers are common keys. A tuple can be a key if all of its contents are themselves suitable hashable values.
Lists and dictionaries cannot be keys because they are mutable and compared by value. A dictionary lookup depends on a key’s hash value remaining constant; changing a key in a way that affects lookup would make that unreliable. Immutability is a useful beginner clue, but the actual requirement is hashability. The Python Language Reference: Data Model explains the relationship between hashing and dictionary lookup.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Values do not have this restriction. A dictionary value can be a list or another dictionary:
profile = {
"name": "Ada",
"skills": ["Python", "math"],
"location": {"city": "London"}
}
How do I loop through a dictionary?
A for loop over a dictionary visits its keys. Use items() to get each key and value together:
for key, value in person.items():
print(key, value)
Use keys() for a view of the keys and values() for a view of the values. Calling list(d) produces a list of the dictionary’s keys.
How do I build dictionaries with comprehensions or combine them?
A dictionary comprehension creates entries from an iterable in a compact expression. This example maps each number to its square:
Best Value
squares = {n: n**2 for n in (2, 4, 6)}
You can also combine mappings with dictionary unpacking. If the same key appears more than once, a later unpacked entry supplies the value:
defaults = {"theme": "light", "font_size": 12}
custom = {"theme": "dark"}
settings = {**defaults, **custom}
print(settings) # {'theme': 'dark', 'font_size': 12}
Are Python dictionaries ordered?
Yes. Python guarantees that dictionaries preserve insertion order from Python 3.7 onward. In CPython 3.6, insertion order was preserved as an implementation detail, rather than a language guarantee.
Insertion order is not alphabetical or numeric sorting. Replacing a value for an existing key does not change that key’s position. If you delete a key and insert it again, it goes to the end. Use sorted(d) when you specifically want sorted keys; the tutorial also distinguishes insertion order from sorting.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




