Simple exercise

Rick Johnson rantingrickjohnson at gmail.com
Mon Mar 14 12:00:07 EDT 2016


On Monday, March 14, 2016 at 10:06:56 AM UTC-5, Oscar Benjamin wrote:
> 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. 

Yes. zip is no doubt more Pythonic than any indexing will
ever be, but without a way to manage this "discarding
issue", i can't justify using the function. There are three
possible ways to solve this dilemma:

  (1) Add a keyword argument to zip, something like
  "validateLengths" -- which will default to False. 
  
  (2) Create a new zip function called "strictzip" which
  will always throw and error when all of the sequences
  don't share the same length. 
  
  (3) Encourage every programmer to write their own wrapper
  around zip.

And since we've recently learned that Python programmers
have an aversion to typing, number three is out the
question.

> 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])

You make a valid point here. So i'm not 100% protected using
indexing, however, i am 100% unprotected using zip. Whew...
I'm still right, but *ONLY* because i'm not 100% wrong.

PS: For second there, i was afraid my impeccable reputation
might have been in jeopardy. O:-)



More information about the Python-list mailing list