0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
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.