Lesson 03
Iterate list, tuple, str, set, and dict — Iterator Type Names
Map list, tuple, str, set, and dict to everyday stories—basket, queue, letters, lottery, keys—then match Python iterator type names.
 as turning a collection into a Next button that reveals one item at a time.
 → Iterator → next() → one value
What you will learn
- Five everyday stories mapped to list, tuple, str, set, and dict iterators
- Type names for list, tuple, str, set, and dict iterators
- Why
iter(123445)raisesTypeError - Dict iterators yield keys — use
x.get(key)for values - File lines as an iterator (
StringIOin the playground)
1. List = Shopping Basket
A basket already holds many items. You pick one product each time — you do not eat everything at once.

print(next(it)) # Apple
print(next(it)) # MilkIterator type: list_iterator
Mixed-type list — while + next:
x = [10, "RS", 23.45, 3+4j, True, False]
itr = iter(x)
print(type(itr)) # <class 'list_iterator'>
while True:
try:
print(next(itr))
except StopIteration:
break2. Tuple = People in a Train Queue
A queue has a fixed order. People do not swap places — you simply call the next passenger.

it = iter(people)
print(next(it)) # AIterator type: tuple_iterator
Tuple example:
x = (10, "RS", True)
itr = iter(x)
print(type(itr)) # <class 'tuple_iterator'>
print(next(itr), next(itr), next(itr))3. String = Reading a Word
When you read PYTHON, you naturally take one letter at a time.
:
print(ch)Iterator type: str_iterator (exact name may vary slightly by Python version)
The same for val in itr pattern works on "PYTHON".
4. Set = Lottery Ball Machine
Lottery balls come out one by one — but the order is unpredictable.
:
print(n)Important: never depend on the printed order.
Iterator type: set_iterator
Set example:
x = {10, "RS", 23.45, 3+4j, True, False}
itr = iter(x)
print(type(itr)) # set_iterator
for val in itr:
print(val)5. Dictionary = Hotel Room Keys
The receptionist hands you room numbers first. You open each room to see the guest name.
:
print(m, rooms.get(m))Key ideas:
iter(dict)→ gives keysrooms.get(key)→ gives values
Iterator type: dict_keyiterator (name may vary by version)
Dict example:
x = {10: "Python", 20: "C", 30: "Java", 40: "C++"}
itr = iter(x)
for val in itr:
print("{} {}".format(val, x.get(val)))Why does iter(12345) fail?
A number is like a single brick. You cannot walk through it because it is not a sequence.

# TypeError: 'int' object is not iterableIf you meant digits as characters, cast first:
for d in str(123445):
print(d)Remember: int is not iterable. The playground demonstrates iter(123445) raising TypeError.
File iterator = Reading a Notebook
A text file behaves like turning notebook pages — you read one line at a time. The playground uses StringIO instead of a file path on disk.

for line in file:
print(line.strip())Output:
Swathi
Rahul
PriyaRun the full mix — list, str, tuple, set, dict, and iter(123445) — in the playground below.
Interview memory table
| Collection | Think of... | Iterator type |
|---|---|---|
| List | Shopping basket | list_iterator |
| Tuple | Train queue | tuple_iterator |
| String | Reading letters | str_iterator |
| Set | Lottery balls | set_iterator |
| Dictionary | Hotel room keys | dict_keyiterator |
| Integer | Single brick | Not iterable |
One-minute visual recap
 works only on iterables — list, tuple, string, set, dictionary, file. It does not work on an int; that raises TypeError.
Next: Generators and yield.
Loading playground…
Check your understanding
Collection iterators quiz
For a list, iter(lst) gives type(itr) as:
1 / 10