Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement __iter__ for a Custom Container

Implement iteration for a container so instances can be iterated over, multiplied by a per-container multiplier.

Python practice25 minMagic Methods & Operator OverloadingAdvancedLast updated April 11, 2026

Problem statement

You are given a CustomContainer class that stores an ordered collection of items and a multiplier. Implement the __iter__ magic method so that iterating over an instance yields each stored item multiplied by the container's multiplier (in the original order). The iterator should work with built-in functions like list(), sum(), and for-loops. Do not modify other methods or attributes.

Task

Write the __iter__ dunder method to allow iteration over stored items with each item scaled by the container's multiplier.

Examples

Basic scaling

Input

list(CustomContainer([1, 2, 3], 2))

Output

[2, 4, 6]

Each element 1,2,3 is multiplied by multiplier 2 producing 2,4,6.

Input format

Calls will construct CustomContainer(items, multiplier) and then iterate over it (e.g. list(container) or sum(container)).

Output format

An iterable sequence of items resulting from item * multiplier, returned as whatever expression demands (list, sum, etc.).

Constraints

Do not change the class signature. Implement only __iter__. The items can be any numeric types; multiplier may be any numeric type as well. Maintain original order.

Samples

Sample 1

Input

list(CustomContainer([0, 5, -2], 3))

Output

[0, 15, -6]

0*3=0, 5*3=15, -2*3=-6