Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 5

Iterate through dictionary keys and values

Easy

8 minute session

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.

Code editor
Loading editor…

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.

07:41 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.