Re: Some things I'd like

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Mon, 8 Aug 1994 17:36:03 -0400

In article <Cu2s67.4wF@rahul.net> you write:
>Here are some things from my Python Wish List:
>
>2. Conversions to and from lists:
> tuple_to_list() list_to_tuple()
> string_to_list() list_to_string()
> Since LIST is the only mutable sequential type (a hindrance, IMHO),
>these conversions will permit easy manipulation of elements.
> These can be implemented in Python, but a built-in would be much
>faster.

>>> string.joinfields( ['a', 'b', 'c', 'd', 'e'], '' ) # list to string
'abcde'

Since string.joinfields is REALLY usually (builtin) strop.joinfields,
this one is effectively a _built-in_ operation.

>>> map( None, 'abcdefg' ) # string to list
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> map( None, ( 1,2,3,4,5,6 ) ) # tuple to list
[1, 2, 3, 4, 5, 6]

There is a note in bltinmodule.c that 'map( None, seq )' could
probably be made into a more effecient special case. Since
map-None is also a quick notation for a transpose operation,
optomizing that case would be useful.

>>> map( None, [1,2,3], [10,20,30], [100,200,300] )
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]

>>> reduce( lambda a,b: a + (b,), 'abcdefg', ()) # string to tuple
('a', 'b', 'c', 'd', 'e', 'f', 'g')
>>> reduce( lambda a,b: a + (b,), [1,2,3,4,5], () ) # list to tuple
(1, 2, 3, 4, 5)

These two are the ones that that take a minute to figure out or
remember, and are neither builtin, nor optimisable in that form.
string-to-tuple wasn't even on your list -- I don't think it is
a very necessary feature. But list-to-tuple is actually one I
could often use: 'apply' takes a function and a *tuple*, and I often
find myself wanting to apply a method to a list of args. ( But maybe
this is not longer true now that we have 'map' ! I haven't hit that
need lately, so maybe it's easier to avoid now. )

So, my vote would be:
- definitely:
optimize the exiting map( None, ... ) special case
- and maybe:
add a fast tuple( seq ) builtin function.

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics