[Tutor] Data persistence problem

Jim Mooney cybervigilante at gmail.com
Sat Jun 22 04:26:53 CEST 2013


On 21 June 2013 16:56, ALAN GAULD <alan.gauld at btinternet.com> wrote:


> if isinstance(dict(),typein):
>    try: newdict = dict(zip(dl[::2],dl[1::2]))
>    except TypeError:
>     raise ValueError("input lists must be an even length")

Not sure why TypeError and ValueError is used. I would have thought
StopIteration but explain your logic on that as I'm unclear. But the
Exception never tripped, either way. I tried different length
iterables in the zip, but it looks like dict knows tostop before it
trip thems. Only next() does raises the exception. Unless I am
confused ;')

>>> zippy = zip([1,2],[3,4,5,6,7,8,9])
>>> D = dict(zippy)
>>> D
{1: 3, 2: 4} # dict works fine
>>> next(zippy) # exhausting zippy raises StopIteration
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
StopIteration

-- 
Jim
A pride of lions, a gaggle of geese, a pack of wolves, a sewer of bankers.


More information about the Tutor mailing list