Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't zip object in Python an iterator?

I'm reading 'Introducing Python' by Bill Lubanovic and it says that

Like zip(), range() returns an iterable object,

But isn't this an incorrect statement? For example,

s= zip([1,2,3],['one','two','three'])
next(s)
>> (1,'one')
next(s)
>> (2,'two')

a = range(10)
next(a)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-7b3ad3809256> in <module>()
      1 a = range(10)
----> 2 next(a)

TypeError: 'range' object is not an iterator

From the code above, we can check that zip() returns an iterator not an iterable object.

like image 634
Jin Avatar asked Dec 06 '25 09:12

Jin


2 Answers

"an iterator" and "iterable" are not the same thing. All iterators are iterable, but not all iterables are iterators.

iterables can be passed to iter(), which returns an iterator. iterators can be passed to next(), and also can be passed to iter(), which acts as the identity function in that case.


https://docs.python.org/glossary.html#term-iterable

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), …). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.

https://docs.python.org/glossary.html#term-iterator

An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

like image 185
Amber Avatar answered Dec 07 '25 23:12

Amber


Iterators are iterable. The fact that zip returns an iterator doesn't mean it doesn't return an iterable. Also, if this book was written for Python 2, both zip and range returned lists in that version.

like image 40
user2357112 supports Monica Avatar answered Dec 07 '25 21:12

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!