Confused with python lists: or aren't they iterators?

I study Alex Marteli Python in a nutshell , and the book assumes that any object that has a next() method, (or at least can be used as) an iterator . He also suggests that most iterators are built by implicit or explicit method calls called iter .

After reading this in the book, I felt a desire to try. I ran the python 2.7.3 interpreter and did the following:

 >>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for number in range(0, 10): ... print x.next() 

However, the result was as follows:

 Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: 'list' object has no attribute 'next' 

In confusion, I tried to study the structure of object x via dir(x) , and I noticed that it has an object of function __iter__ . Therefore, I realized that it can be used as an iterator if it supports this type of interface.

So, when I tried again, this time a little differently, trying to do this:

 >>> _temp_iter = next(x) 

I got this error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list object is not an iterator 

But as a NOT list, it can be an iterator, since it seems to support this interface and can definitely be used as one in the following context:

 >>> for number in x: ... print x 

Can someone help me clarify this in my mind?

+42
python iterator list
Oct 24 '12 at 17:00
source share
4 answers

They are repeatable, but they are not iterators. They can be passed to iter() to get an iterator for them either implicitly (for example, via for ), or explicitly, but they themselves are not iterators.

+61
Oct 24
source share

You need to first convert the list to an iterator using iter() :

 In [7]: x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [8]: it=iter(x) In [9]: for i in range(10): it.next() ....: ....: Out[10]: 0 Out[10]: 1 Out[10]: 2 Out[10]: 3 Out[10]: 4 Out[10]: 5 Out[10]: 6 Out[10]: 7 Out[10]: 8 Out[10]: 9 In [12]: 'next' in dir(it) Out[12]: True In [13]: 'next' in dir(x) Out[13]: False 

checking if an object is an iterator or not:

 In [17]: isinstance(x,collections.Iterator) Out[17]: False In [18]: isinstance(x,collections.Iterable) Out[18]: True In [19]: isinstance(it,collections.Iterable) Out[19]: True In [20]: isinstance(it,collections.Iterator) Out[20]: True 
+17
Oct 24
source share

Just in case, you are confused about the difference between iterators and iterators. An iterator is an object that represents a data stream. It implements the iterator protocol:

  • __iter__ method
  • next method

Repeated calls to the iterators next () return consecutive elements in the stream. when there is no more data, the iterator object is exhausted, and any further calls to its next () will simply call StopIteration again.

On the other hand, iterative objects implement the __iter__ method, which, when called, returns an iterator that allows multiple passes over their data. Iterable objects are reusable, after exhaustion they can be repeated again. They can be converted to iterators using the iter function.

So, if you have a list (iterable), you can do:

 >>> l = [1,2,3,4] >>> for i in l: ... print i, 1 2 3 4 >>> for i in l: ... print i, 1 2 3 4 

If you convert your list to an iterator:

 >>> il = l.__iter__() # equivalent to iter(l) >>> for i in il: ... print i, 1 2 3 4 >>> for i in il: ... print i, >>> 
+12
Oct 24
source share

The list is not an iterator, but the list contains the __iter__ iterator __iter__ , so when you try to use the __iter__ method to loop in any list, it gets the iterator object and then it uses the list's next () method.

 x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] it = x.__iter__() 

Now it contains an iterator object x , which you can use as it.next() until a StopIteration exception is thrown

+1
Jul 23 '14 at 12:47
source share



All Articles