Python Tutorials - Herong's Tutorial Examples
∟Iterators, Generators and List Comprehensions
This chapter provides introductions and tutorial examples about iterators, generators and list comprehensions. Topics include introduction on iterable objects, iterators, generators, and list comprehensions; list of built-in iterable data types; using 'yield' statements and generator functions; using generator expressions and list comprehensions.
These sections are omitted from this Web preview version. To view the full content,
see information on how to obtain the full version this book.
What Is Iterator Object
What Is Iterable Object
Iterable Objects of Built-in Data Types
What Is Generator Iterator
What Is Generator Expression
What Is Filtered Generator Expression
What Is Double-Generator Expression
What Is List Comprehension
What Is Filtered List Comprehension
What Is Double-List Comprehension
Generator Expression vs. List Comprehension
Takeaways:
- An iterator object supports a required instance method, iterator.__next__(), to return the next item.
- An iterable object supports a required instance method, iterable.__iter__(), to return an iterator object.
- isinstance(object, collections.abc.Iterable) can be used to
test if a given object is iterable or not.
- For built-in iterable objects, their iterators are also iterable.
- A generator iterator is an iterator object created by a generator function
or a generator expression.
- A generator function is a special function that contains one or more "yield" statements.
- A "yield" statement transforms the enclosing function into
a generator function. It suspends and resumes execution to support next(generator) calls.
- A generator expression transforms an expression into a generator with a "for" clause enclosed in parentheses.
- A filtered generator expression is a generator expression with an "if" sub-clause
to filter out items with a given condition.
- A double-generator expression is a generator expression that contains a double "for" clause
to iterate two iterables sequentially.
- A list comprehension creates a list object using a "for" clause enclosed in square brackets.
- A filtered list comprehension is a list comprehension with an "if" sub-clause
to filter out items with a given condition.
- A double-list comprehension is a list comprehension that contains a double "for" clause
to iterate two iterables sequentially.
- Generator expressions and list comprehensions have almost identical syntaxes:
(EXPRESSION for ...) vs. [EXPRESSION for ...]
- Generator expressions return "generator" objects.
- List comprehensions return "list" objects.
- Generator expressions use "late-binding" to support next(generator) calls
to reduce memory usage.
Table of Contents
About This Book
Running Python Code Online
Python on macOS Computers
Python on Linux Computers
Built-in Data Types
Variables, Operations and Expressions
Statements - Execution Units
Function Statement and Function Call
►Iterators, Generators and List Comprehensions
Classes and Instances
Modules and Module Files
Packages and Package Directories
"sys" and "os" Modules
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
Anaconda - Python Environment Manager
Jupyter Notebook and JupyterLab
References
Full Version in PDF/EPUB