using lambda to print everything in a list

Bjorn Pettersen BPettersen at NAREX.com
Thu Apr 26 20:49:36 EDT 2001


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

	def write(x):
	   print x

	map(lambda x: write(x), ['line one', 'line two'])

however,

	for x in ['line one', 'line two']:
	   print x

is much clearer IMHO.

-- bjorn

-----Original Message-----
From: Jeff Shipman [mailto:shippy at nmt.edu]
Sent: Thursday, April 26, 2001 6:26 PM
To: python-list at python.org
Subject: using lambda to print everything in a list


I've started to discover the real handiness with
lambda expressions in python. I would like to
print out each string in a list without having
a for loop and I thought this was the perfect
candidate for lambda. This is what I'm doing:

map(lambda x: print x, ['line one', 'line two'])

and I get the following error:

  File "<stdin>", line 1
    map(lambda x: print x, ['line one', 'line two'])
                      ^
SyntaxError: invalid syntax

I can do it fine with things like string.replace, but
for some reason does it not like built-in functions?
Is there a way to do what I'm wanting to do? Or must
I use a for loop?

Thanks in advance,
 
Jeff "Shippy" Shipman     E-Mail: shippy at nmt.edu
Computer Science Major    ICQ: 1786493
New Mexico Institute of Mining and Technology
Homepage: http://www.nmt.edu/~shippy
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list