20

In Python what is equivalent to Ruby's Array.each method? Does Python have a nice and short closure/lambda syntax for it?

[1,2,3].each do |x|
  puts x
end

5 Answers 5

42

Does Python have a nice and short closure/lambda syntax for it?

Yes, but you don't want it in this case.

The closest equivalent to that Ruby code is:

new_values = map(print, [1, 2, 3])

That looks pretty nice when you already have a function lying around, like print. When you just have some arbitrary expression and you want to use it in map, you need to create a function out of it with a def or a lambda, like this:

new_values = map(lambda x: print(x), [1, 2, 3])

That's the ugliness you apparently want to avoid. And Python has a nice way to avoid it: comprehensions:

new_values = [print(x) for x in values]

However, in this case, you're just trying to execute some statement for each value, not accumulate the new values for each value. So, while this will work (you'll get back a list of None values), it's definitely not idiomatic.

In this case, the right thing to do is to write it explicitly—no closures, no functions, no comprehensions, just a loop:

for x in values:
    print x
Sign up to request clarification or add additional context in comments.

4 Comments

This is probably the best and most complete answer since it actually addressed the issues of lambdas stated in the question which my answer did not. +1
It's probably worth pointing out that my specific examples only work in Python 3.x (or, in 2.6-2.7, if you do a from __future__ import print_function); maybe I should have used sys.stdout.write or something as a rough equivalent of puts instead of print to avoid this… But if you understood everything, I guess it's good enough.
each doesn't return any value, unlike map.
@AlexanderSupertramp: Right, but Python doesn't have an "each". And, more importantly, it doesn't have any expression syntax for mutating things when you don't care about the return value. And that's intentional. If you want a value, you can do it in an expression; if you don't, there's no reason not to just use a statement, as my answer says.
12

The most idiomatic:

for x in [1,2,3]:
    print x

Comments

2

You can use numpy for vectorized arithmetic over an array:

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> a * 3
array([3, 6, 9])

You can easily define a lambda that can be used over each element of an array:

>>> array_lambda=np.vectorize(lambda x: x * x)
>>> array_lambda([1, 2, 3])
array([1, 4, 9])

But as others have said, if you want to just print each, use a loop.

Comments

1

There are also libraries that wrap objects to expose all the usual functional programming stuff.

E.g. pydash allows you to do things like this:

>>> from pydash import py_
>>> from __future__ import print_function
>>> x = py_([1,2,3,4]).map(lambda x: x*2).each(print).value()
2
4
6
8
>>> x
[2, 4, 6, 8]

(Just always remember to "trigger" execution and/or to un-wrap the wrapped values with .value() at the end!)

1 Comment

What I personally like about writing code like this is that it reads left-to-right. It's easier to follow the data/value flows.
1

without need of an assignment:

list(print(_) for _ in [1, 2, 3])

or just

[print(_) for _ in [1, 2, 3]] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.