Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

dict.popitem

Removes and returns the most recently inserted remaining key and value.

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

Syntax

m.popitem()

Removes and returns the most recently inserted remaining key and value.

Insertion order determines the pair. Calling this method on an empty dictionary raises KeyError.

The most recently inserted remaining pair is removed first. Since b follows a, the returned pair contains b and eight and the original dictionary retains a. Calling the method on an empty dictionary raises KeyError.

d = {"a": 1, "b": 2}
d["a"] = 3
assert d.popitem() == ("b", 2)

Examples

m = {"a": 3}
m["b"] = 8
print(m.popitem())
print(m)

Result

('b', 8)
{'a': 3}

The most recently inserted remaining pair is removed first. Since b follows a, the returned pair contains b and eight and the original dictionary retains a. Calling the method on an empty dictionary raises KeyError.

Sources