Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108411 > unrolled thread

Is there a reason zip() wipes out data?

Started byDFS <nospam@dfs.com>
First post2016-05-09 00:22 -0400
Last post2016-05-08 23:06 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Is there a reason zip() wipes out data? DFS <nospam@dfs.com> - 2016-05-09 00:22 -0400
    Re: Is there a reason zip() wipes out data? Dan Sommers <dan@tombstonezero.net> - 2016-05-09 04:29 +0000
    Re: Is there a reason zip() wipes out data? Paul Rubin <no.email@nospam.invalid> - 2016-05-08 23:06 -0700

#108411 — Is there a reason zip() wipes out data?

FromDFS <nospam@dfs.com>
Date2016-05-09 00:22 -0400
SubjectIs there a reason zip() wipes out data?
Message-ID<ngp34a$m4i$1@dont-email.me>
python 2.7.11 docs: "The returned list is truncated in length to the 
length of the shortest argument sequence."


a = ['who','let','the']
b = ['dogs','out?']
c = zip(a,b)

print c
[('who', 'dogs'), ('let', 'out?')]


Wouldn't it be better to return an empty element than silently kill your 
data?

[('who', 'dogs'), ('let', 'out?'), ('the', '')]




Edit: I see they addressed this in 3.5 (maybe earlier), with an option:

"itertools.zip_longest(*iterables, fillvalue=None)

Make an iterator that aggregates elements from each of the iterables. If 
the iterables are of uneven length, missing values are filled-in with 
fillvalue. Iteration continues until the longest iterable is exhausted."

[toc] | [next] | [standalone]


#108412

FromDan Sommers <dan@tombstonezero.net>
Date2016-05-09 04:29 +0000
Message-ID<ngp3o2$lam$1@dont-email.me>
In reply to#108411
On Mon, 09 May 2016 00:22:46 -0400, DFS wrote:

> python 2.7.11 docs: "The returned list is truncated in length to the 
> length of the shortest argument sequence."
> 
> 
> a = ['who','let','the']
> b = ['dogs','out?']
> c = zip(a,b)
> 
> print c
> [('who', 'dogs'), ('let', 'out?')]
> 
> 
> Wouldn't it be better to return an empty element than silently kill your 
> data?
> 
> [('who', 'dogs'), ('let', 'out?'), ('the', '')]

>>> dfs_zip([1, 2, 3], [4, 5])
[(1, 4), (2, 5), (3, '')]

No, that's not much better, thanks.

In the face of ambiguity, refuse the temptation to guess.

[toc] | [prev] | [next] | [standalone]


#108415

FromPaul Rubin <no.email@nospam.invalid>
Date2016-05-08 23:06 -0700
Message-ID<87twi76a0h.fsf@jester.gateway.pace.com>
In reply to#108411
DFS <nospam@dfs.com> writes:
> Edit: I see they addressed this in 3.5 (maybe earlier), with an option:
> "itertools.zip_longest(*iterables, fillvalue=None)

This is available in 2.7 as itertools.izip_longest

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web