Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

dict.get

Retrieves the value of a key or returns a supplied default.

Version: Python 3.14; repository image, production runtime not attested

Syntax

m.get(key, default=None, /)

Retrieves the value of a key or returns a supplied default.

Reading a missing key this way leaves the dictionary unchanged.

For an existing key the stored value wins, even if it is None. For a missing key return the optional fallback without inserting anything. The example reads three for a, nine for z, and confirms z is still absent.

d = {"x": None}
assert d.get("x", 7) is None and d.get("y", 7) == 7 and "y" not in d

Examples

m = {"a": 3}
print(m.get("a", 9))
print(m.get("z", 9))
print("z" in m)

Result

3
9
False

For an existing key the stored value wins, even if it is None. For a missing key return the optional fallback without inserting anything. The example reads three for a, nine for z, and confirms z is still absent.

Sources