Problem No 5
Iterate through dictionary keys and values
Easy≈ 8 minute session
Lesson guide
What this Python exercise practices
Iterate through dictionary keys and values is a beginner practice lesson that focuses on loops, iteration, counters. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Lists or strings
- Basic for loop syntax
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Practice path
Summary
Access key-value pairs in a dictionary and return them as a list of tuples preserving insertion order.
Problem statement
Given a dictionary, return a list containing (key, value) tuples for each entry in the dictionary. The order should reflect the dictionary's insertion order. The function should not print anything; it should return the list so that it can be tested.
Task
Implement a function that returns the dictionary's items as a list of (key, value) tuples in insertion order.
Examples
Simple dictionary
Input
iterate_dict({'a': 1, 'b': 2})
Output
[('a', 1), ('b', 2)]
Explanation
The items are returned as a list of tuples in insertion order.
Input format
A single dictionary passed to iterate_dict(d).
Output format
A list of tuples [(key1, value1), (key2, value2), ...].
Constraints
Dictionary may be empty. Keys and values can be any types that can be stored in a dict.
Samples
Sample input 0
iterate_dict({1: 2, 3: 4})
Sample output 0
[(1, 2), (3, 4)]
Explanation 0
Numbers are returned as (key, value) tuples in the same order they were written.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.