[Python-Dev] zip() and list-comprehension with commas
Ken Manheimer
klm@digicool.com
Wed, 12 Jul 2000 19:54:59 -0400 (EDT)
On Wed, 12 Jul 2000, Paul Prescod wrote:
> Skip Montanaro wrote:
> >
> > BAW> Alternatives (as Greg I think brought up): pairwise(), maybe
> > BAW> zipper() for zip() since it's more evocative.
> >
> > "zip" already has a lot of compression connotations. How about "stitch", as
> > in "stitch two lists together"?
>
> "Stitch" and "with" are okay, but I'd like to hear someone debunk the
> names used today by MAL. They seemed very logical to me. I'd just change
> the semantics to be lazy in a couple of cases.
>
> >>> tuple([1,2,3])
> (1,2,3)
>
> >>> tuples([1,2,3],[4,5,6])
> <tuple generator: ((1,4),(2,5),(3,6)>
>
> >>> list((1,2,3))
> [1,2,3]
>
> >>> lists((1,2,3),(4,5,6))
> <list generator: [1,4],[2,5],[3,6]>
>
> >>> dict([(1,2),(3,4),(5,6)])
> {1:2,3:4,5:6}
How about this different name, with a somewhat different approach:
Name is "furl", with inverse "unfurl". First argument is the target-type
of the result:
>>> furl([], (1, 2, 3), [4, 5, 6,])
[[1,4], [2,5], [3,6]]
>>> furl((), [1,2,3],(4,5,6))
((1,4), (2,5), (3,6))
>>> furl({}, (1,2,3), "abc")
{1:'a', 2:'b', 3:'c'}
>>> unfurl([], [1,4], [2,5], [3,6])
[[1, 2, 3], [4, 5, 6]]
>>> unfurl((), [1,4], [2,5], [3,6])
((1, 2, 3), (4, 5, 6))
>>> unfurl((), {1:'a', 2:'b', 3:'c'})
((1, 2, 3), ('a', 'b', 'c'))
Being explicit about the type of the result, rather than keying on the
type of both arguments, means you're not surprised by an exception when
one of your vars had a tuple and the other had a list. Better explicit
than implicit, ay?
I'm not at all opposed yielding generators, like in skip's example.
Seems like comprehensions and simple notations for infinite generators -
"[1..]" - would deliver the benefits of concise, arbitrarily elaborate
stream sources where xrange, alone, falls way short.
I really like "furl" - it may help to appreciate the charm if you say it
out loud a few times: "furl". "furl". "furl". Ahhh. :-)
Ken