Dict Methods

dict.keys()

Provides a live view of hashable dictionary keys. Traversal follows insertion order, and a new iteration reflects later edits. The view supports set-like operations and can accept an iterable as the other operand of a set operator. It is not a list and does not support positional indexing. Take a list snapshot before deleting keys during traversal; structural edits during iteration may raise RuntimeError or skip entries.

Formal Syntax & Structure

python syntaxpython
mapping.keys()

Return Value

dict_keys

Reusable dynamic set-like view of keys.

Working Example

Example Snippetpython
stock = {"pen": 2, "book": 3}keys = stock.keys()stock["bag"] = 1print(list(keys))print(sorted(keys & ["book", "bag"]))
Python Reference