"Jeff Shipman" <shippy at nmt.edu> wrote in message news:3AE8BCA9.386446CD at nmt.edu... > I've started to discover the real handiness with > lambda expressions in python. I would like to Maybe one day you'll explain it to me as well...? My personal impression is that lambda is a (minor) nuisance. Naming the code fragment that you want to pass to some other function, by making it a nested function, seems clearer & handier to me. > 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 ...because, as I see others have explained, 'print' is a statement. lambda only takes an expression. To use statements, you have to write a function then pass it -- the only "cost" (is it a cost?-) is to give that nested local function a name. So, as already seen in another post: def emit(x): print x map(emit, ['line one', 'line two']) > I can do it fine with things like string.replace, but > for some reason does it not like built-in functions? Functions are fine for map, statements aren't. > Is there a way to do what I'm wanting to do? Or must > I use a for loop? A simple loop would probably be clearer, but a named function will let you use map() if you insist:-). Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4