Python equivalent to Ruby Array.each method
Solution 1
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
Solution 2
The most idiomatic:
for x in [1,2,3]:
print x
Solution 3
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.
Solution 4
There are also libraries that wrap objects to expose all the usual functional programming stuff.
- PyDash http://pydash.readthedocs.org/en/latest/
- underscorepy (Google
github underscore.py
)
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!)
Solution 5
without need of an assignment:
list(print(_) for _ in [1, 2, 3])
or just
[print(_) for _ in [1, 2, 3]]
Related videos on Youtube
BuddyJoe
I like to code C# and work with the web. Still learning.
Updated on July 09, 2022Comments
-
BuddyJoe almost 2 years
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
-
Shashank over 10 yearsThis 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
-
abarnert over 10 yearsIt'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 usedsys.stdout.write
or something as a rough equivalent ofputs
instead ofprint
to avoid this… But if you understood everything, I guess it's good enough. -
Incerteza about 9 yearseach doesn't return any value, unlike map.
-
abarnert about 9 years@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.
-
Ru Hasha over 8 yearsWhat I personally like about writing code like this is that it reads left-to-right. It's easier to follow the data/value flows.