What are "iterable", "iterator", and "iteration" in Python? How are they defined?
See also: How to build a basic iterator?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.
In Python, iterable and iterator have specific meanings.
An iterable is an object that has an
__iter__method which returns an iterator, or which defines a__getitem__method that can take sequential indexes starting from zero (and raises anIndexErrorwhen the indexes are no longer valid). So an iterable is an object that you can get an iterator from.An iterator is an object with a
next(Python 2) or__next__(Python 3) method.Whenever you use a
forloop, ormap, or a list comprehension, etc. in Python, thenextmethod is called automatically to get each item from the iterator, thus going through the process of iteration.A good place to start learning would be the iterators section of the tutorial and the iterator types section of the standard types page. After you understand the basics, try the iterators section of the Functional Programming HOWTO.