I'm kind of confused over the iterator object/concept and the yield statement and its usage.

According to my understanding, all the python sequence are iterators, as they don't need the classic for (int i=0; i<list.length; i++) to iterate through lists/arrays.

Am I correct over this? Am I missing anything?

Also how would i use the yield statement? (not getting it from the official explanation)

Dani AI

Generated

A few focused clarifications that build on and :

Sequences (list, tuple, str) are iterable objects, not iterators. An iterable implements __iter__() which returns a separate iterator object. An iterator implements the iterator protocol (in Python 3: __iter__() and __next__()) and is what the for loop actually asks for behind the scenes. Some objects (generator objects, file objects) are their own iterators (calling iter(obj) returns obj), while container sequences return fresh iterator objects each time.

Generators are a convenient way to create iterator objects. A generator function (contains yield) returns a generator object when called; that object is an iterator which produces values lazily and maintains the function’s local state between yields (as noted). Generator objects are single-use: to iterate again, call the generator function again or materialize results (for example, with list(...)). Generator expressions provide a concise syntax for simple streams:

squares = (i*i for i in range(5))
print(next(squares))   # 0
print(next(squares))   # 1

If implementing an iterator by hand, the class must return an iterator from __iter__() and supply __next__() that raises StopIteration when done. Example (Python 3):

class CountToN:
    def __init__(self, n):
        self.n = n
        self.i = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.i >= self.n:
            raise StopIteration
        self.i += 1
        return self.i

Practical notes: prefer generators for large or streaming data to save memory; use list() to cache results if reuse is required; check iter(obj) is obj to detect single-use iterators. For advanced usage see the Python docs on iterator types and the yield expression: Iterator Types and Yield expression.

Recommended Answers

All 4 Replies

I'm kind of confused over the iterator object/concept and the yield statement and its usage.

According to my understanding, all the python sequence are iterators, as they don't need the classic for (int i=0; i<list.length; i++) to iterate through lists/arrays.

Am I correct over this? Am I missing anything?

Also how would i use the yield statement? (not getting it from the official explanation)

Java... to iterate through list/arrays you can still do

for (type var : arr) {
    body-of-loop
}

I think you mean "generators" in Python.

So you have

def my_gen():
    yield 1
    yield "hello"
    yield "world"
    yield 100

for x in my_gen():
    print x

Generator is function which 'freezes' for every result and yields it, when next answer is requested function is 'unfrozen' with all the local state. When all answers are finished the iterator gives StopIteration exception and does not give more results.

Sequences that are iterable have method called __iter__ and that is called when they are used as iterators. So this is equivalent to normal iteration of list:

for i in iter(['a','b','c']): print i

iter makes iterator explicitly.

More info on the __iter__ method:

class Alphabet(object):
    def __iter__(self):  #This is the __iter__ magic method
        for letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            yield letter

myalph = Alphabet()

for l in myalph:
    print l


class WeirdList(list):
    def __init__(self, *args, **kwargs):
        if args:
            self._l = args[0]
        super(WeirdList, self).__init__(*args,**kwargs)
    def __iter__(self):
        for x in self._l[::2]:
            yield x

myweirdlist = WeirdList((1,2,3,4,5,6,7,8,9))
for x in myweirdlist:
    print x

ah that makes a lot of sense. thanks jcao

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.