Simple exercise

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Mar 14 11:06:15 EDT 2016


On 14 March 2016 at 14:35, Rick Johnson <rantingrickjohnson at gmail.com> wrote:
>
> I would strongly warn anyone against using the zip function
> unless
...
> I meant to say: absolutely, one hundred percent *SURE*, that
> both sequences are of the same length, or, absolutely one
> hundred percent *SURE*, that dropping values is not going to
> matter. For that reason, i avoid the zip function like the
> plague. I would much rather get an index error, than let an
> error pass silently.

I also think it's unfortunate that zip silently discards items. Almost
always when I use zip I would prefer to see an error when the two
iterables are not of the same length. Of course you're not necessarily
safer with len and range:

a = [1, 2, 3]
b = 'abcde'

for n in range(len(a)):
    print(a[n], b[n])

--
Oscar



More information about the Python-list mailing list