Iteration and Files
Core Python

Iteration and Files

Write shorter, faster loops with comprehensions, iterators and itertools, and work with files, context managers and paths inside your browser's temporary file system.

  1. 1Python List ComprehensionsA list comprehension builds a new list in a single expression by describing what each item should be and, opt…
  2. 2Python Generators, Iterators and yieldA generator is a function that uses yield to hand out values one at a time, pausing after each one and resumi…
  3. 3Python IteratorsAn iterator is an object that hands out the items of a sequence one at a time through next(), remembers where…
  4. 4Python map, filter and reducemap() transforms every item of an iterable, filter() keeps only the items that pass a test, and functools.red…
  5. 5Python any() and all()any() answers "is at least one item true?" and all() answers "is every item true?" for a whole iterable, and …
  6. 6Python itertoolsitertools is a standard-library toolbox of fast, lazy iterator building blocks for chaining, slicing, groupin…
  7. 7Python File HandlingFile handling means using open() to connect your program to a file, reading text from it or writing text into…
  8. 8Python with StatementThe with statement runs setup code before a block and guaranteed cleanup code after it, even when the block f…
  9. 9Python pathlibpathlib's Path is an object that represents a file or folder location, with operators and methods to join, ta…

Start with Python List Comprehensions →

About Iteration and Files

Iteration is how Python walks through data, and files are where data lives. You will write list comprehensions, understand iterators and generators, combine tools from itertools, use any and all, and read and write files safely with the with statement and pathlib. Together they let you process large inputs without loading everything into memory.

Related reference: iterator vs generator · File handling cheat sheet · Read a file line by line

Further reading (official documentation)

Practice for this topic