[Tutor] Program gets stuck after a creating a list from dictinary items!

Steven D'Aprano steve at pearwood.info
Sat Jul 7 06:12:21 CEST 2012


Prasad, Ramit wrote:

> I believe that the usage of 'in <blah>' converts it into a set (or 
> set-like) object so probably that is the same as set(list(set())).

No, certainly not. That would be terribly inefficient, since it means first 
iterating over blah entirely to convert it into a set, and then iterating over 
the set. That does twice as much work as just iterating over blah once.

Besides, sets cannot contain duplicates. If you convert a list to a set, you 
lose any duplicate values in the set.


What 'for x in <blah>' does is first try to use the iterator protocol, and if 
that doesn't work, it tries the sequence protocol, and if that doesn't work it 
raises TypeError.

The iterator protocol looks something like this, implemented in fast C code:


try:
     it = iter(blah)  # build an iterator object from blah
except TypeError:
     fall back on Sequence Protocol
else:
     try:
         while True:  # loop forever
             x = next(it)  # ask the iterator object for the next value
             process x
     except StopIteration:
         for loop is now done


The advantage here is that building an iterator object need not walk over the 
entire data structure in advance. Iterators should walk over it lazily, only 
as needed, rather than all at once like converting into a set requires.


The Sequence Protocol is the older version, going *way* back to the Python 1.x 
series, again implemented in fast C code. It looks something like this:

i = 0
while True:  # loop forever
     try:
         x = blah[i]  # get the first item, the second, third, fourth, ...
         process x
         i += 1
     except IndexError:
         for loop is now done




-- 
Steven


More information about the Tutor mailing list