Skip to main content

How to Get a Value from a Dictionary by Its Key in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Python dictionary maps keys to values, and you read a value by putting its key in square brackets: book["title"]. When the key might be missing, book.get("author") returns None, or a default you choose, instead of stopping with an error.

Syntax​

Syntax
<value> = <dictionary>[<key>]
<value> = <dictionary>.get(<key>)
<value> = <dictionary>.get(<key>, <default>)
PartWhat it does
<dictionary>[<key>]The value stored under <key>. Raises KeyError when there's no such key.
.get(<key>)The value, or None when the key is missing.
.get(<key>, <default>)The value, or <default> when the key is missing.

The Python documentation for dictionaries: d[key] "Return the item of d with key key. Raises a KeyError if key is not in the map." And get: "Return the value for key if key is in the dictionary, else default."

The Example​

The example reads a book's title with square brackets, reads a missing author with get, once without and once with a default, and finally reads author with square brackets.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which creates a book with title Dune and year 1965, prints book with title in brackets, book.get of author, book.get of author with the default unknown, and book with author in brackets.

main.py
book = {"title": "Dune", "year": 1965}

print(book["title"])
print(book.get("author"))
print(book.get("author", "unknown"))
print(book["author"])

Step 2: Run It​

Terminal
uv run python main.py
Output
Dune
None
unknown
Traceback (most recent call last):
File "/Users/Shared/you/main.py", line 6, in <module>
print(book["author"])
~~~~^^^^^^^^^^
KeyError: 'author'

A zsh terminal where uv run python main.py prints Dune, None and unknown, then a traceback ending in KeyError: 'author'.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • book["title"] found the key and returned Dune.
  • book.get("author") didn't find it and returned None, and with a default it returned unknown.
  • book["author"] stopped the program with KeyError: 'author', naming the key it couldn't find.

Common Mistakes​

MistakeWhat happensDo this instead
Using square brackets for a key that may be missingKeyError stops the programUse .get(key) or .get(key, default)
Getting the case wrongKeys are case-sensitive: book["Title"] raised KeyError: 'Title'Use the exact key
Using .get for a key that must be thereA missing key turns into None and fails later, far from the causeUse square brackets so the error shows up right away

Troubleshooting​

SymptomLikely causeFix
KeyError: '...'The dictionary has no such keyCheck the keys with print(book.keys()), or use .get
None where you expected a value.get didn't find the keyCheck the spelling, or pass a default

FAQ​

How do I check whether a key exists?​

Use in: "author" in book returned False.

Can the key be something other than a string?​

Yes. Numbers, tuples and other unchangeable values can be keys.

Conclusion​

dictionary[key] reads a value and raises KeyError when the key is missing. dictionary.get(key, default) returns the default instead, so choose brackets for keys that must exist and get for keys that may not.

Additional Resources​