using lambda to print everything in a list
Duncan Booth
duncan at NOSPAMrcp.co.uk
Fri Apr 27 04:38:28 EDT 2001
Bjorn Pettersen <BPettersen at NAREX.com> wrote in
<mailman.988332640.18020.python-list at python.org>:
> Lambda only takes an expression and print is a statement (which is why
> you're getting a syntax error). If you really want to do this (and I'm
> not condoning it) you can do:
>
> import sys
> map(lambda x: sys.stdout.write(x), ['line one', 'line two'])
or get rid of the lambda entirely:
map(sys.stdout.write, ['line one', 'line two'])
except that neither of these options puts a newline between the elements of
the list. This does though:
list = ['line one', 'line two']
[ sys.stdout.write(x+'\n') for x in list]
>
> or
>
> def write(x):
> print x
>
> map(lambda x: write(x), ['line one', 'line two'])
map(write, ['line one', 'line two'])
>
> however,
>
> for x in ['line one', 'line two']:
> print x
>
> is much clearer IMHO.
Agreed.
[bottom quote snipped - please don't bottom quote your messages]
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list