need help with list/variables
jgardner at jonathangardner.net
Tue Dec 30 23:00:39 CET 2008
On Dec 30, 11:41 am, 5lvqbw... at sneakemail.com wrote:
> >>> conc = lambda x,y: x[:] + y # concatenate 2 lists without side effects
> >>> mylist = ['something\n', 'another something\n', 'something again\n']
> >>> myvar = reduce(conc, mylist)
> >>> print myvar
"conc"? "side effects"? Missing Lisp much? ;-)
Let's try to Pythonize your lisp.
Assigning a lambda to a variable? Just use def. It's pretty much the
>>> def conc(x,y): return x[:]+y
I don't think x+y affects x at all when x and y are lists. (Is that a
lispism?) So x[:]+y has an unnecessary array copy.
>>> def conc(x,y): return x+y
Python may still be slow at string concatenation. (Perl is fast.)
Rather than concatenating one at a time, it's better, I understand, to
build up a list and then join() them together.
The other posters got the right answer in the pythonic way.
More information about the Python-list