Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. dict.keys() / dict.values() / dict.items()

dict.keys() / dict.values() / dict.items()

Methods that return the keys, values, and key-value pairs of a dictionary as view objects. They are commonly used for iteration with for loops and set operations.

Syntax

# Returns a view of all keys in the dictionary.
dict.keys()

# Returns a view of all values in the dictionary.
dict.values()

# Returns a view of all key-value pairs (tuples) in the dictionary.
dict.items()

Methods

MethodDescription
dict.keys()Returns a view object (dict_keys) of all keys in the dictionary. The view automatically reflects any changes to the dictionary.
dict.values()Returns a view object (dict_values) of all values in the dictionary. Duplicate values are all included.
dict.items()Returns a view object (dict_items) of all (key, value) tuples in the dictionary.

Sample Code

person = {
    "name": "Alice",
    "age": 20,
    "city": "New York",
    "job": "Engineer"
}

# Iterate over keys using keys().
print(list(person.keys()))  # ['name', 'age', 'city', 'job']
for key in person.keys():
    print(key, end=" ")  # name age city job
# Note: "for key in person:" produces the same result.

# Iterate over values using values().
print(list(person.values()))  # ['Alice', 20, 'New York', 'Engineer']
for value in person.values():
    print(value, end=" ")  # Alice 20 New York Engineer

# Iterate over key-value pairs using items() (the most common pattern).
for key, value in person.items():
    print(f"{key}: {value}")
# name: Alice
# age: 20
# city: New York
# job: Engineer

# Calculate the total and average of scores using values().
scores = {"Math": 85, "English": 92, "History": 78, "Science": 88}
total = sum(scores.values())
average = total / len(scores)
print(f"Total: {total}, Average: {average:.1f}")  # Total: 343, Average: 85.8

# Set operations using keys().
dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"b": 20, "c": 30, "d": 40}

# Get the keys that exist in both dictionaries.
common_keys = dict1.keys() & dict2.keys()
print(common_keys)  # {'b', 'c'}

# Get the keys that exist in dict1 but not in dict2.
only_in_dict1 = dict1.keys() - dict2.keys()
print(only_in_dict1)  # {'a'}

# Practical example: transform a dictionary using items().
# Swap keys and values.
original = {"a": 1, "b": 2, "c": 3}
inverted = {v: k for k, v in original.items()}
print(inverted)  # {1: 'a', 2: 'b', 3: 'c'}

# Build a dictionary with only the entries that meet a condition.
filtered = {k: v for k, v in scores.items() if v >= 85}
print(filtered)  # {'Math': 85, 'English': 92, 'Science': 88}

# View objects dynamically reflect changes to the dictionary.
d = {"x": 1}
keys_view = d.keys()
print(list(keys_view))  # ['x']
d["y"] = 2
print(list(keys_view))  # ['x', 'y'] (reflects the updated dictionary)

Notes

keys(), values(), and items() each return a view object — a "window" into the dictionary that automatically updates whenever the dictionary changes. To use the result as a list, convert it with list().

Iterating over a dictionary directly with for key in dict: produces the same result as using keys(). Adding or removing entries from a dictionary while iterating over it raises a RuntimeError. If you need to modify the dictionary during iteration, first create a list of the keys (list(dict.keys())) and iterate over that instead.

Since Python 3.7, dictionaries maintain insertion order, so keys() returns keys in the order they were inserted. For safe key lookup in a dictionary, see dict.get() / dict.setdefault().

If you find any errors or copyright issues, please .