https://github.com/python/cpython/commit/1d39dba759049ee330b5b35d8f0f46f2a283bc4c
commit: 1d39dba759049ee330b5b35d8f0f46f2a283bc4c
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2025-10-06T13:11:49Z
summary:

[3.13] gh-116488: Mention `dict.get` in the data structures tutorial 
(GH-139643) (GH-139656)

gh-116488: Mention `dict.get` in the data structures tutorial (GH-139643)
(cherry picked from commit 69cfad0b3e1e2805914cb647cce4c7c2bfed5910)

Co-authored-by: Cycloctane <[email protected]>

files:
M Doc/tutorial/datastructures.rst

diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index db8a066b369a83..1332c53f39687e 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -512,8 +512,12 @@ dictionary; this is also the way dictionaries are written 
on output.
 The main operations on a dictionary are storing a value with some key and
 extracting the value given the key.  It is also possible to delete a key:value
 pair with ``del``. If you store using a key that is already in use, the old
-value associated with that key is forgotten.  It is an error to extract a value
-using a non-existent key.
+value associated with that key is forgotten.
+
+Extracting a value for a non-existent key by subscripting (``d[key]``) raises a
+:exc:`KeyError`. To avoid getting this error when trying to access a possibly
+non-existent key, use the :meth:`~dict.get` method instead, which returns
+``None`` (or a specified default value) if the key is not in the dictionary.
 
 Performing ``list(d)`` on a dictionary returns a list of all the keys
 used in the dictionary, in insertion order (if you want it sorted, just use
@@ -528,6 +532,12 @@ Here is a small example using a dictionary::
    {'jack': 4098, 'sape': 4139, 'guido': 4127}
    >>> tel['jack']
    4098
+   >>> tel['irv']
+   Traceback (most recent call last):
+     File "<stdin>", line 1, in <module>
+   KeyError: 'irv'
+   >>> print(tel.get('irv'))
+   None
    >>> del tel['sape']
    >>> tel['irv'] = 4127
    >>> tel

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to