Python 3 reference · Dictionary Methods

Python Dictionary pop() Method

Remove a key and return its value. Returns the default if given and the key is missing; otherwise raises KeyError.

Python 3 Since 3.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

dict.pop(key)
dict.pop(key, default)

Parameters

ParameterDescription
key The key to remove.
default optional Returned if the key is missing.

Return value

The removed value.

Examples

Remove and use a valueTry it yourself
user = {"name": "Ada", "password": "secret"}
pw = user.pop("password")
print(user, pw)

Output

{'name': 'Ada'} secret
With a defaultTry it yourself
opts = {"debug": True}
print(opts.pop("verbose", False), opts)

Output

False {'debug': True}

Version history

VersionChange
Python 3.0Added

Was this page helpful?