<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni" -->

---
title: What `for x in y` hides from you | daily.dev
description: Python's `for x in y` loop hides a three-step iterator protocol: calling `iter()` to get an iterator, repeatedly calling `next()` to retrieve values, and...
canonical: https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: What `for x in y` hides from you | daily.dev
og:description: Python's `for x in y` loop hides a three-step iterator protocol: calling `iter()` to get an iterator, repeatedly calling `next()` to retrieve values, and...
og:url: https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni
og:image: https://api.daily.dev/og/posts/rgnrVwNNI.png
og:image:alt: What `for x in y` hides from you
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# What `for x in y` hides from you

**[Hacker News](https://daily.dev/sources/hn)** · 7 min read · 0 upvotes · 0 comments

## Summary

Python's `for x in y` loop hides a three-step iterator protocol: calling `iter()` to get an iterator, repeatedly calling `next()` to retrieve values, and stopping on `StopIteration`. This unified protocol explains why for loops work across lists, strings, ranges, generators, and custom objects implementing `__iter__` and `__next__`. The author discovered this while building Memphis, a Python interpreter in Rust, and illustrates the distinction between iterables (which produce fresh iterators) and iterators (which are one-way). Understanding this model clarifies behaviors like exhausted iterators, generator pausing, and tuple unpacking in loops.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://fromscratchcode.com/blog/what-for-x-in-y-hides-from-you>

## Similar posts on daily.dev

- [All iteration is the same in Python](https://daily.dev/posts/all-iteration-is-the-same-in-python-kmgraqtz3) · Planet Python · 1 upvotes · 0 comments
- [Exploring Asynchronous Iterators and Iterables – Real Python](https://daily.dev/posts/exploring-asynchronous-iterators-and-iterables-real-python-9rkdwe6ag) · Real Python · 1 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"What `for x in y` hides from you","url":"https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni"},"datePublished":"2026-07-15T14:55:45.246Z","dateModified":"2026-07-15T15:26:45.516Z","description":"Python's `for x in y` loop hides a three-step iterator protocol: calling `iter()` to get an iterator, repeatedly calling `next()` to retrieve values, and...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/da820e7bdad1a5345f8a5f8c40961830?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/da820e7bdad1a5345f8a5f8c40961830?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Hacker News","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Hacker News","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/hn","url":"https://daily.dev/sources/hn"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/what-for-x-in-y-hides-from-you-rgnrvwnni","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,rust","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Hacker News","item":"https://daily.dev/sources/hn"},{"@type":"ListItem","position":3,"name":"What `for x in y` hides from you"}]}
```

