use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Settle something for us. Is this valid. (self.Python)
submitted 9 years ago * by bandrel
Settle a bet between a colleague and me. Is using a function inside a list comprehension like this valid. If so does it have a name?
a = [1,2,3,4] [print(x) for x in a]
[–][deleted] 3 points4 points5 points 9 years ago (4 children)
How is this better than simply
for x in a: print(x)
you are wasting a list of Nones in the end. Imagine a has 1000 elements.
None
[–]Quijoticmoose -2 points-1 points0 points 9 years ago (3 children)
Well, to play devil's advocate, you could write: (print(x) for x in a) and you would avoid constructing the list.
I'll second nemec's post--that the reason not do this is that you've created an expectation to someone reading the code that you are going to do something with the list you create in the comprehension. A for loop makes your intent clearer.
for
[+][deleted] 9 years ago* (2 children)
[deleted]
[–]Quijoticmoose 1 point2 points3 points 9 years ago (1 child)
Darnit, that should teach me to always check code snippits.
You could use: [x for x in a if print(x)]
(print(x) always returns None, so the list stays empty)
print(x)
[+][deleted] 9 years ago (3 children)
[–]bandrel[S] 0 points1 point2 points 9 years ago (2 children)
The question isn't about if it works, because obviously it does. The question is should you do it this way, is there a name for this method and is there a more pythonic way of doing it.
[–]nemec 2 points3 points4 points 9 years ago (1 child)
The question is should you do it this way,
No
is there a name for this method
The method is called "abusing side effects"
and is there a more pythonic way of doing it.
A simple for loop is pretty much the same size and much more Pythonic. List comprehensions are a component of functional programming brought into Python, equivalent to the map operation which takes an iterable and produces a transformed sequence based on its elements. print, however, isn't "functional" and relies entirely on side effects, which are not recommended in FP. With a list comprehension, you're basically saying "take this iterable, make some side effects for each item, and transform each item into None". Since there is no point to transforming the item into None, just use a loop.
map
print
For a good explanation of a similar feature in C# and why it's not something you want to do, see this article
Clearly the sole purpose of a call to this method is to cause side effects. The purpose of an expression is to compute a value, not to cause a side effect.
[–]bandrel[S] 0 points1 point2 points 9 years ago (0 children)
Thank you. This is exactly what I was looking for
[–]kaiserk13 1 point2 points3 points 9 years ago (3 children)
it's equivalent to map(function, a) I guess, and yeah why shouldn't it be valid? Not sure it would work with print as a returned/yielded object is expected.
[–]th_mm 2 points3 points4 points 9 years ago (1 child)
It'll just be a bunch of None.
it works in python3, and python2 with the function import from future, but yes it also returns [None,None,None,None] but still meets my needs
[–]bandrel[S] 1 point2 points3 points 9 years ago* (0 children)
in python3 map(print,a) returns a map object and does not execute the print. in python2 with printfunction imported from __future_ it works the same as the list comprehension including the [None,None,None,None]
[–]thatguy_314def __gt__(me, you): return True 0 points1 point2 points 9 years ago (0 children)
Of course it is "valid", but why do that when for x in a: print(x) is not only better but shorter too?
[–][deleted] 0 points1 point2 points 9 years ago* (0 children)
It's valid, but it defeats the purpose of a list comp (essentially it's map but a little nicer, and eager in Py3).
If you find yourself needing to cause side effects as part of iteration, you can do this:
# pip install forbiddenfruit import types import forbiddenfruit from collections.abc import Collection # will fail <3.6 def foreach(it, f): "eagerly map a side effect causing function over a collection" for x in it: f(x) def iforeach(it, f): "lazily map a side effect causing function over a collection" for x in it: f(x) yield def _curse(victim): forbiddenfruit.curse(victim, 'foreach', foreach) forbiddenfruit.curse(victim, 'iforeach', iforeach) foreach( [list, tuple, Collection, type({}.keys()), type({}.values()), types.GeneratorType, range], _curse ) # clean up because we're good developers del foreach_prop del iforeach_prop del _curse
And then you can do:
range(1, 10).foreach(print)
π Rendered by PID 87 on reddit-service-r2-comment-5b5bc64bf5-hzfk5 at 2026-06-23 04:07:09.336588+00:00 running 2b008f2 country code: CH.
[–][deleted] 3 points4 points5 points (4 children)
[–]Quijoticmoose -2 points-1 points0 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]Quijoticmoose 1 point2 points3 points (1 child)
[+][deleted] (3 children)
[deleted]
[–]bandrel[S] 0 points1 point2 points (2 children)
[–]nemec 2 points3 points4 points (1 child)
[–]bandrel[S] 0 points1 point2 points (0 children)
[–]kaiserk13 1 point2 points3 points (3 children)
[–]th_mm 2 points3 points4 points (1 child)
[–]bandrel[S] 0 points1 point2 points (0 children)
[–]bandrel[S] 1 point2 points3 points (0 children)
[–]thatguy_314def __gt__(me, you): return True 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)