W3docs

Python Dictionaries: Accessing Items

Learn every way to access Python dictionary items: bracket notation, get(), keys(), values(), items(), membership testing, and nested dict access.

Python dictionaries store data as key-value pairs. Knowing how to read back those values — and what happens when a key is missing — is the foundation of working with any dictionary. This chapter covers every standard access technique: bracket notation, the .get() method, iterating with .keys(), .values(), and .items(), and testing for membership with in.

Bracket Notation

The most direct way to retrieve a value is to write the dictionary name followed by the key in square brackets.

Access a dictionary value by key

python— editable, runs on the server

Python looks up 'Bob' in the hash table and returns its value in O(1) time, regardless of how many keys the dictionary contains.

KeyError when the key is missing

If the key does not exist, Python raises a KeyError and stops execution.

ages = {'Alice': 27, 'Bob': 34, 'Charlie': 45}

print(ages['Dave'])  # KeyError: 'Dave'

Always handle this case — either by checking membership first (see the in operator section below) or by using .get() instead.

The .get() Method

.get(key) returns the value for key if it exists, and None otherwise — no exception is raised.

Use .get() to safely access a dictionary key

python— editable, runs on the server

Providing a default value

Pass a second argument to .get() to receive a fallback instead of None:

colors = {'apple': 'red', 'banana': 'yellow', 'grape': 'purple'}

color = colors.get('orange', 'unknown')
print(color)  # unknown

This is the idiomatic way to access a key that may or may not be present without wrapping your code in a try/except block.

Membership Testing with in

Use the in operator to check whether a key exists before accessing it:

ages = {'Alice': 27, 'Bob': 34, 'Charlie': 45}

if 'Alice' in ages:
    print(ages['Alice'])  # 27

print('Dave' in ages)   # False
print('Bob' in ages)    # True

in tests only keys, not values. It runs in O(1) time because dictionaries are hash-based.

Accessing All Keys, Values, and Items

Python dictionaries expose three view objects that let you iterate or inspect their contents without constructing a separate list.

.keys() — all keys

colors = {'apple': 'red', 'banana': 'yellow', 'grape': 'purple'}

print(colors.keys())
# dict_keys(['apple', 'banana', 'grape'])

for fruit in colors.keys():
    print(fruit)
# apple
# banana
# grape

.values() — all values

colors = {'apple': 'red', 'banana': 'yellow', 'grape': 'purple'}

print(colors.values())
# dict_values(['red', 'yellow', 'purple'])

for color in colors.values():
    print(color)
# red
# yellow
# purple

.items() — key-value pairs

.items() returns each entry as a (key, value) tuple. Tuple unpacking makes this the most useful view for most iteration tasks:

colors = {'apple': 'red', 'banana': 'yellow', 'grape': 'purple'}

for fruit, color in colors.items():
    print(f'{fruit} is {color}')
# apple is red
# banana is yellow
# grape is purple

View objects are dynamic — they reflect the current state of the dictionary. If you add or remove a key after creating a view, the view updates automatically.

Accessing Items in a Nested Dictionary

When a dictionary value is itself a dictionary, chain square brackets (or .get() calls) to reach the inner value.

Access values in a nested dictionary

python— editable, runs on the server

For safer access through multiple levels, chain .get() calls:

title = library.get('book4', {}).get('title', 'Not found')
print(title)  # Not found

See the Nested Dictionaries chapter for a deeper treatment of multi-level data structures.

Choosing the Right Access Method

SituationRecommended approach
Key is guaranteed to existd[key] — clear and fast
Key might be missing, None is fined.get(key)
Key might be missing, need a fallbackd.get(key, default)
Check before accessingif key in d: d[key]
Iterate all entriesfor k, v in d.items()

What Comes Next

Once you can read values from a dictionary, the natural next steps are:

Practice

Practice
In Python, which principles are applied to access items from a list or a dictionary?
In Python, which principles are applied to access items from a list or a dictionary?
Was this page helpful?