# What Python's for x in y Loop Actually Hides from You

Original title: What \`for x in y\` hides from you – From Scratch Code

> Hacker News story 48920156 · language: en · submitted 2026-07-15
> Web version: https://zeli.app/story/48920156
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 6 · by [rbanffy](<https://news.ycombinator.com/user?id=rbanffy>) · 💬 2
[original](<https://fromscratchcode.com/blog/what-for-x-in-y-hides-from-you/>) · [HN discussion](<https://news.ycombinator.com/item?id=48920156>)

## Summary

Building my Python interpreter in Rust, Memphis, revealed that the familiar for x in y loop isn't just iterating over a list. Instead, it strictly follows a protocol by calling iter() and next() until StopIteration is raised. This simple mechanism explains why Python handles lists, strings, ranges, and generators so uniformly, turning a magical syntax into a clear, predictable interaction between the loop and any iterable object.

## Key quote

> for x in y does not mean loop over y. It means ask y how to be iterated.

## HN discussion (2 comments)

### [tantalor](<https://news.ycombinator.com/item?id=48921764>)

> \> Compared to i++ from C/C++ or forEach from JavaScript, Python's version just works.
>
> Comparing to forEach in JS is incorrect because forEach is an method of Array.
>
> You should compare it to `for...of` in JS. Both operate on iterators.
>
> Article is missing an important distinction between iterators and other "array like" types (including strings):
>
> Iterators don't have to stop, e.g., they can take from a generator that never ceases.
>
> Both Python and JS are happy to loop forever if the iterator never stops.

### [anthonj](<https://news.ycombinator.com/item?id=48921100>)

> I don't really get the point of the article. Even if I knew little about python, would be it surpsing that a language with no real basic types is probably abstracting a lot?
>
> Even a simple i=0, i=i+1 is "hiding" a lot in python then.

### [WhyNotHugo](<https://news.ycombinator.com/item?id=48920743>)

> 'for' loops in Rust do the same: they create an iterator and then iterate over that.
>
> You can write the exact same loop with `let mut iter = v.iter(); while Some(x) = iter.next()`.
>
> 'for' loops in Rust are purely syntax sugar, and I somewhat wish they didn't exist. They provide you two ways of doing the same thing, but one of them hides the details from you. Having 'for' as a keyword is nice for folks coming from other languages, but then it hides the possibility of other interesting usages, like cloning an iterator inside a loop.

---

## Cite

- Page: https://zeli.app/story/48920156
- Date: 2026-07-15
- Source: fromscratchcode.com (https://fromscratchcode.com/blog/what-for-x-in-y-hides-from-you/)
- Hacker News: https://news.ycombinator.com/item?id=48920156

## Other languages

- zh: https://zeli.app/zh/story/48920156 · markdown: https://zeli.app/zh/story/48920156.md
- ja: https://zeli.app/ja/story/48920156 · markdown: https://zeli.app/ja/story/48920156.md
- ko: https://zeli.app/ko/story/48920156 · markdown: https://zeli.app/ko/story/48920156.md
- de: https://zeli.app/de/story/48920156 · markdown: https://zeli.app/de/story/48920156.md
- es: https://zeli.app/es/story/48920156 · markdown: https://zeli.app/es/story/48920156.md
