Lisp mentality vs. Python mentality

Peter Otten __peter__ at web.de
Sun Apr 26 09:31:56 EDT 2009


Arnaud Delobelle wrote:

> def equal_items(iter1, iter2, key=lambda x: x):
>     iter1, iter2 = iter(iter1), iter(iter2)
>     for x, y in izip(iter1, iter2):
>         if key(x) != key(y):
>             return False
>     for x, y in izip_longest(iter1, iter2):
>         return False
>     return True
> 
> (untested)

This will fail when iter1 yields one more item than iter2. izip() then
consumes one extra item:

>>> from itertools import izip
>>> a = iter([1,2])
>>> list(izip(a, "b"))
[(1, 'b')]
>>> a.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Peter



More information about the Python-list mailing list