What is the preferred way to tell someone "I want to apply func
to each element in iterable
for its side-effects"?
Option 1... clear, but two lines.
for element in iterable:
func(element)
Option 2... even more lines, but could be clearer.
def walk_for_side_effects(iterable):
for element in iterable:
pass
walk_for_side_effects(map(func, iterable)) # Assuming Python3's map.
Option 3... builds up a list, but this how I see everyone doing it.
[func(element) for element in iterable]
I'm liking Option 2; is there a function in the standard library that is already the equivalent?
We say that the function changeit has a side effect on the list object that is passed to it. Global variables are another way to have side effects. For example, similar to examples you have seen above, we could make double have a side effect on the global variable y.
Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.
You don't need recursion if you only work with 2D lists. I know you wrote you don't want to use any for loop , but map is nothing more than a disguised for loop . It also doesn't mutate the list.
Yes. map always returns a map object. No matter the iterable you are napping over. It is meant to work with any iterable.
Avoid the temptation to be clever. Use option 1, it's intent is clear and unambiguous; you are applying the function func()
to each and every element in the iterable.
Option 2 just confuses everyone, looking for what walk_for_side_effects
is supposed to do (it certainly puzzled me until I realized you needed to iterate over map()
in Python 3).
Option 3 should be used when you actually get results from func()
, never for the side effects. Smack anyone doing that just for the side-effects. List comprehensions should be used to generate a list, not to do something else. You are instead making it harder to comprehend and maintain your code (and building a list for all the return values is slower to boot).
This has been asked many times, e.g., here and here. But it's an interesting question, though. List comprehensions are meant to be used for something else.
Other options include
map()
- basically the same as your samplefilter()
- if your function returns None, you will get an empty listfor
-loopwhile the plain loop is the preferable way to do it. It is semantically correct in this case, all other ways, including list comprehension, abuse concepts for their side-effect.
In Python 3.x, map()
and filter()
are generators and thus do nothing until you iterate over them. So we'd need, e.g., a list(map(...))
, which makes it even worse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With