Bird
Raised Fist0
Pythonprogramming~10 mins

Dictionary keys, values, and items in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Dictionary keys, values, and items
Start with dictionary
Call .keys() method
Get all keys as view
Call .values() method
Get all values as view
Call .items() method
Get all key-value pairs as tuples
End
This flow shows how to get keys, values, and key-value pairs from a dictionary using its methods.
Execution Sample
Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
values = my_dict.values()
items = my_dict.items()
print(keys)
print(values)
print(items)
This code creates a dictionary and prints its keys, values, and items.
Execution Table
StepActionExpression EvaluatedResult/Output
1Create dictionarymy_dict = {'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}
2Get keys viewkeys = my_dict.keys()dict_keys(['a', 'b', 'c'])
3Get values viewvalues = my_dict.values()dict_values([1, 2, 3])
4Get items viewitems = my_dict.items()dict_items([('a', 1), ('b', 2), ('c', 3)])
5Print keysprint(keys)dict_keys(['a', 'b', 'c'])
6Print valuesprint(values)dict_values([1, 2, 3])
7Print itemsprint(items)dict_items([('a', 1), ('b', 2), ('c', 3)])
8EndNo more codeExecution complete
💡 All dictionary views printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
my_dictundefined{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}{'a': 1, 'b': 2, 'c': 3}
keysundefinedundefineddict_keys(['a', 'b', 'c'])dict_keys(['a', 'b', 'c'])dict_keys(['a', 'b', 'c'])dict_keys(['a', 'b', 'c'])
valuesundefinedundefinedundefineddict_values([1, 2, 3])dict_values([1, 2, 3])dict_values([1, 2, 3])
itemsundefinedundefinedundefinedundefineddict_items([('a', 1), ('b', 2), ('c', 3)])dict_items([('a', 1), ('b', 2), ('c', 3)])
Key Moments - 3 Insights
Why do keys, values, and items show as dict_keys, dict_values, and dict_items instead of lists?
These are special view objects that reflect the dictionary's current state. They are not lists but can be converted to lists if needed. See execution_table rows 2-4 where these views are created.
Can the dictionary change after creating keys, values, or items views?
Yes, these views reflect changes in the dictionary dynamically. If you add or remove items from the dictionary after creating these views, the views will update automatically.
How do I get a list of keys, values, or items if I want to use them like a list?
You can convert the views to lists using list(). For example, list(my_dict.keys()) will give a list of keys. This is useful if you want to index or modify the collection.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'keys' after Step 3?
Adict_keys(['a', 'b', 'c'])
Bdict_values([1, 2, 3])
Cdict_items([('a', 1), ('b', 2), ('c', 3)])
Dundefined
💡 Hint
Check the 'keys' variable value in variable_tracker after Step 3.
At which step does the program print the dictionary's values?
AStep 7
BStep 5
CStep 6
DStep 4
💡 Hint
Look at execution_table rows where print statements happen.
If you convert items to a list, what type of elements will the list contain?
AStrings of keys only
BTuples of (key, value) pairs
CValues only
DIntegers only
💡 Hint
Refer to execution_table row 4 where items are shown as dict_items of tuples.
Concept Snapshot
Dictionary keys, values, and items methods:
- keys() returns a view of all keys
- values() returns a view of all values
- items() returns a view of (key, value) tuples
These views reflect dictionary changes dynamically.
Convert to list() to use like a list.
Full Transcript
This lesson shows how to get keys, values, and items from a Python dictionary. We start by creating a dictionary with three pairs. Then we call .keys(), .values(), and .items() methods to get views of keys, values, and key-value pairs. These views are special objects that show the current dictionary contents. We print each view to see their output. The views update if the dictionary changes. To use them like lists, convert with list(). This helps you access dictionary parts easily and understand how Python stores them.

Practice

(1/5)
1. Which method would you use to get all the keys from a Python dictionary my_dict?
easy
A. my_dict.values()
B. my_dict.get()
C. my_dict.items()
D. my_dict.keys()

Solution

  1. Step 1: Understand dictionary methods

    The keys() method returns all keys in the dictionary.
  2. Step 2: Match method to requirement

    Since we want all keys, my_dict.keys() is the correct method.
  3. Final Answer:

    my_dict.keys() -> Option D
  4. Quick Check:

    keys() = my_dict.keys() [OK]
Hint: Keys come from keys(), values from values(), pairs from items() [OK]
Common Mistakes:
  • Confusing keys() with values()
  • Using get() which retrieves a single value
  • Using items() which returns key-value pairs
2. Which of the following is the correct syntax to get all values from a dictionary data?
easy
A. data.values()
B. data.get_values()
C. data.values
D. values(data)

Solution

  1. Step 1: Recall method syntax

    Dictionary methods require parentheses to call them, so values() is correct.
  2. Step 2: Check each option

    data.values misses parentheses, get_values() and values(data) are invalid.
  3. Final Answer:

    data.values() -> Option A
  4. Quick Check:

    values() needs parentheses [OK]
Hint: Always add () to call dictionary methods like values() [OK]
Common Mistakes:
  • Forgetting parentheses after method name
  • Using non-existent methods like get_values()
  • Trying to call values() as a function with dictionary argument
3. What is the output of this code?
my_dict = {'a': 1, 'b': 2}
print(list(my_dict.items()))
medium
A. [('a', 1), ('b', 2)]
B. ['a', 'b']
C. [1, 2]
D. Error

Solution

  1. Step 1: Understand items() method

    The items() method returns key-value pairs as tuples.
  2. Step 2: Convert items to list

    Using list() converts these pairs into a list of tuples: [('a', 1), ('b', 2)].
  3. Final Answer:

    [('a', 1), ('b', 2)] -> Option A
  4. Quick Check:

    items() = list of (key, value) pairs [OK]
Hint: items() returns pairs; list() shows them as list of tuples [OK]
Common Mistakes:
  • Thinking items() returns only keys or only values
  • Expecting a dictionary instead of list of tuples
  • Confusing items() with keys() or values()
4. Find the error in this code snippet:
my_dict = {'x': 10, 'y': 20}
for key, value in my_dict.keys():
    print(key, value)
medium
A. Syntax error in for loop
B. keys() returns only keys, cannot unpack into two variables
C. Missing parentheses after print
D. No error, code runs fine

Solution

  1. Step 1: Check what keys() returns

    keys() returns only keys, so each item is a single value, not a pair.
  2. Step 2: Understand unpacking in for loop

    The loop tries to unpack each key into two variables, causing an error.
  3. Final Answer:

    keys() returns only keys, cannot unpack into two variables -> Option B
  4. Quick Check:

    keys() = keys only, no pairs [OK]
Hint: keys() gives one value per item; items() gives pairs [OK]
Common Mistakes:
  • Trying to unpack keys() into two variables
  • Confusing keys() with items()
  • Assuming keys() returns key-value pairs
5. You have a dictionary grades = {'Alice': 85, 'Bob': 92, 'Charlie': 78}. Which code snippet correctly prints each student's name and grade using dictionary methods?
hard
A. for name, grade in grades.values(): print(name, grade)
B. for name, grade in grades.keys(): print(name, grade)
C. for name, grade in grades.items(): print(name, grade)
D. for grade in grades.values(): print(grade)

Solution

  1. Step 1: Identify method to get pairs

    items() returns key-value pairs, perfect for name and grade.
  2. Step 2: Check each option

    for name, grade in grades.values(): print(name, grade) unpacks grades.values() (single values): error. for name, grade in grades.keys(): print(name, grade) unpacks grades.keys() (single keys): error. for grade in grades.values(): print(grade) prints only grades. for name, grade in grades.items(): print(name, grade) correctly unpacks grades.items() pairs.
  3. Final Answer:

    for name, grade in grades.items(): print(name, grade) -> Option C
  4. Quick Check:

    items() gives key-value pairs for easy unpacking [OK]
Hint: Use items() to loop over keys and values together [OK]
Common Mistakes:
  • Using values() when keys and values needed
  • Unpacking values() which are single values
  • Using keys() without accessing values