What method on a dictionary returns all keys as a view object?

The story behind the answer

The dictionary method that returns all keys as a view object is `keys`. Calling `my_dict.keys()` produces a dynamic view of the dictionary’s keys rather than a separate list copy.

For example, if `scores = {'Ava': 91, 'Leo': 84}`, then `scores.keys()` represents `Ava` and `Leo`. The view can be iterated over, tested for membership, or converted explicitly with `list(scores.keys())` when a list is required. Because it is dynamic, the view reflects later additions and removals from the underlying dictionary.

The other methods have distinct purposes. `items()` returns `(key, value)` pairs, `values()` returns the values, and `get()` retrieves the value associated with one particular key. Since Python 3.7, dictionaries guarantee insertion order as a language behavior, so keys are iterated in insertion order. Keys views are also set-like when their keys are hashable—which dictionary keys must be—allowing operations such as intersection and union.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: