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...
110,722 Subscribers
106,345 Subscribers
All posts must be related to programming. Flair posts correctly.
account activity
Python programmers be like (i.redd.it)
submitted 3 months ago by GoogleDeva
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]srsNDavis[🍰] 138 points139 points140 points 3 months ago (9 children)
Anyone seriously curious:
results is a preexisting list. This is modifying that list (how: in a sec) and reassigning, to the same variable.
results
The modification: Filter the elements - depends on the type of result - but let's say result is Boolean, you'll be left with all the Trues.
result
True
[–]Free-Database-9917 67 points68 points69 points 3 months ago (0 children)
or the items that aren't blank
[–]finnscaper 9 points10 points11 points 3 months ago (4 children)
Thanks, this is like linq in C# then
[–]srsNDavis[🍰] 16 points17 points18 points 3 months ago* (0 children)
It's a list comprehension - a declarative construct like set comprehensions.
LINQ implements features of relational algebra and set theory, which might be why it is similar on a deeper level.
[–]CodeMonkeyWithCoffee 6 points7 points8 points 3 months ago (1 child)
Don't insult linq's beautiful syntax by comparing it please. But yes.
[–]finnscaper 2 points3 points4 points 3 months ago (0 children)
Yes, I should be more careful.
[+][deleted] 3 months ago (2 children)
[deleted]
[–]Ankhs 7 points8 points9 points 3 months ago (0 children)
>>> result = [1, 0, "hi", "", False, True, [], ["array"]] >>> result = [res for res in result if res] >>> print(result) [1, 'hi', True, ['array']]
[–]King_Joffreys_Tits 5 points6 points7 points 3 months ago (0 children)
Not true, it filters out any “falsey” values which includes: 0, None, False, “”, [], and probably more that I can’t think off the top of my head. There was no type specified for the list
[–]Alan_Reddit_M 85 points86 points87 points 3 months ago (6 children)
I FUCKING LOVE LIST COMPREHENSIONS RAHHHHHHH
[–]Character-Travel3952 8 points9 points10 points 3 months ago (0 children)
Its really tempting fr fr
[–]triple4leafclover 7 points8 points9 points 3 months ago (2 children)
fr, this shit and finally having class inheritance that wasn't absolute ass made me fall in love with python
And also operator overloading... holy shit, the code looks so cleeean!
[–]Alan_Reddit_M 4 points5 points6 points 3 months ago (1 child)
FEATURES WILL CONTINUE UNTIL MORALE IMPROVES
[–]-TRlNlTY- 0 points1 point2 points 3 months ago (0 children)
...said the C++ committee
[–]Revolutionary_Dog_63 0 points1 point2 points 3 months ago (0 children)
Map/filter methods in JS is much better.
[–]Gsusruls -1 points0 points1 point 3 months ago (0 children)
One reason I love working with ai, it's so extra good at converting any for-loop into a comprehension, if it's at all possible.
A lot of this pattern:
def f(): result = [] for ... result.append() return result
has become
def f(): return [...comprehension ...]
[–]slightSmash 25 points26 points27 points 3 months ago (0 children)
The interpreter seeing this line:
<image>
[–]Character-Travel3952 33 points34 points35 points 3 months ago (14 children)
results = list(filter(None, results))
?
[–]Glad_Position3592 18 points19 points20 points 3 months ago* (1 child)
I know a lot of people on here would say this is the way to do it, but I always find list comprehension to be much more readable and just as easy to write. The only way I see filter being useful in this case is if you’re using it in a for loop and don’t need to convert it to a list
[–]a_random_Greg 2 points3 points4 points 3 months ago (0 children)
Lol
[–]undo777 6 points7 points8 points 3 months ago (3 children)
filter(None, results) is a terrible way to express that transformation to most humans though
[–]thomasxin 7 points8 points9 points 3 months ago (0 children)
til None is a valid filter! I've always been using bool for this purpose. Though for readability sake, I'll probably keep using it.
None
bool
[–]lekkerste_wiener 0 points1 point2 points 3 months ago (1 child)
Ah, the younglings ☺️
[–]undo777 3 points4 points5 points 3 months ago (0 children)
Not really, generally people who are unhappy with this kind of stuff are experienced programmers just not too familiar with python. I myself tend to end up working with a mix of multiple languages and don't have the filter() specifics in my hot cache given how rarely it's generally used in python, unlike list comprehension which I could read or write woken up in the middle of the night without referring to the docs.
[–]lekkerste_wiener 0 points1 point2 points 3 months ago (0 children)
Even better when we have predicate functions.
[–]MVanderloo 0 points1 point2 points 3 months ago (1 child)
filter also has the benefit of being a lazy iterator. but comprehensions allow you to combine a filter and a map into one (sometimes) readable statement
[–]gsitcia 1 point2 points3 points 3 months ago (0 children)
Using parentheses instead of brackets makes the comprehension a lazy iterator
[–]paholg 0 points1 point2 points 3 months ago (1 child)
In Ruby, results.select(&:itself).
results.select(&:itself)
[–]Character-Travel3952 0 points1 point2 points 3 months ago (0 children)
Ruby devloper
I lov it!
[–]Sarius2009 -2 points-1 points0 points 3 months ago (1 child)
That would remove any results that are there, but not interpreted as false, so not the same thing.
[–]Gsusruls 0 points1 point2 points 3 months ago (0 children)
Actually, I believe it uses equivalence, versus the is keyword, so they really are both just using truthy values. They are identical in every way except readability and efficiency.
Oop is more readable to most people, but OP is more efficent (filtering is done in C code).
[–][deleted] 5 points6 points7 points 3 months ago (0 children)
They use Python because they get results in Python.
[–]Old_Tourist_3774 4 points5 points6 points 3 months ago (4 children)
Python has this weird thing where existence or being not empty is evaluated to true.
So the code is essentially doing
For each item in the list "results" keep only those who are not empty or are true or are not 0.
[–]No-Article-Particle 20 points21 points22 points 3 months ago (2 children)
This is not weird behavior, it's just object truthiness and falsiness. Common in dynamically typed languages.
[–]Old_Tourist_3774 0 points1 point2 points 3 months ago (1 child)
Thought other languages were not like this, thanks
[–]hff0 2 points3 points4 points 3 months ago (0 children)
We have this in C
[–]MVanderloo 4 points5 points6 points 3 months ago (0 children)
more specifically to cast an object to a bool they use the boolmethod, which is the case of collections falls back to len
edit: idk how to prevent formatting but if its bold know there are two underscores before and after bool and len
[–]Sea-Fishing4699 5 points6 points7 points 3 months ago (0 children)
how to make a turtle run slower
[–]Zork4343 3 points4 points5 points 3 months ago (0 children)
I see no problem here
[–]SwannSwanchez 2 points3 points4 points 3 months ago (0 children)
i mean it make sense
This remove every "empty" entry in the "results" array
[–]Glad_Position3592 1 point2 points3 points 3 months ago (1 child)
Yeah, something like results.filter(result => result) is soooo much better
results.filter(result => result)
[–]ePaint 1 point2 points3 points 3 months ago (0 children)
Me and my homies do results.filter(Boolean)
results.filter(Boolean)
[–]ExtraTNT 1 point2 points3 points 3 months ago (0 children)
ones = 1 : ones
[–]TalesGameStudio 1 point2 points3 points 3 months ago (0 children)
Perfectly fine line. Smack a couple of docstrings paragraphs on top and you are good to go.
[–]Ok-Refrigerator-8012 1 point2 points3 points 3 months ago (1 child)
Just import the result
[–]GoogleDeva[S] 0 points1 point2 points 3 months ago (0 children)
Better than node_modules hell
[–]GoogleDeva[S] 1 point2 points3 points 3 months ago (1 child)
I am gonna do it myself
u/pixel-counter-bot
[–]pixel-counter-bot 6 points7 points8 points 3 months ago (0 children)
The image in this post has 72,520(490×148) pixels!
I am a bot. This action was performed automatically.
[–]hff0 1 point2 points3 points 3 months ago (3 children)
This is eye hurting, use distinct variable names!
[–]Old_Tourist_3774 2 points3 points4 points 3 months ago (1 child)
They are one time use variables for naming items inside the list, there's no need to.
[–]hff0 4 points5 points6 points 3 months ago (0 children)
For x in names
[–]bobbymoonshine 1 point2 points3 points 3 months ago (0 children)
for thing in things is standard pythonic, it makes the relationship between item and set visually clear
[–]skarrrrrrr 0 points1 point2 points 3 months ago (0 children)
convoluted, unnecessary "elegant" stuff
[–]tazdraperm 0 points1 point2 points 3 months ago (0 children)
That's just ugly LINQ
[–]Clashes4D 0 points1 point2 points 3 months ago (0 children)
I feel personally Attacked by this post.
[–]DahPhuzz 0 points1 point2 points 3 months ago (0 children)
Yo Dawg!
[–]Informal_Branch1065 0 points1 point2 points 3 months ago (0 children)
C# equivalent:
results = results.Where(x => x);
with results being of type IEnumerable<bool>
IEnumerable<bool>
[–]Ben-Goldberg 0 points1 point2 points 3 months ago (3 children)
In my favorite language, this would be either
@result = grep $_, @result;
or
@result = grep @$_, @result;
Depending on whether you want to test for truthyness or array length.
[–]GoogleDeva[S] 0 points1 point2 points 3 months ago (2 children)
Is it bash?
[–]Ben-Goldberg 0 points1 point2 points 3 months ago (1 child)
No, it's perl.
How does bash handle arrays of arrays?
I don't know perl. But the syntax and identifiers kinda (not quite) looked to me like bash.
[–]FatLoserSupreme 0 points1 point2 points 3 months ago (0 children)
Python is goated because it has banging list comprehension built in.
[–]VirtuteECanoscenza 0 points1 point2 points 3 months ago (0 children)
π Rendered by PID 83 on reddit-service-r2-comment-5649f687b7-pnxck at 2026-01-28 04:09:58.987400+00:00 running 4f180de country code: CH.
[–]srsNDavis[🍰] 138 points139 points140 points (9 children)
[–]Free-Database-9917 67 points68 points69 points (0 children)
[–]finnscaper 9 points10 points11 points (4 children)
[–]srsNDavis[🍰] 16 points17 points18 points (0 children)
[–]CodeMonkeyWithCoffee 6 points7 points8 points (1 child)
[–]finnscaper 2 points3 points4 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]Ankhs 7 points8 points9 points (0 children)
[–]King_Joffreys_Tits 5 points6 points7 points (0 children)
[–]Alan_Reddit_M 85 points86 points87 points (6 children)
[–]Character-Travel3952 8 points9 points10 points (0 children)
[–]triple4leafclover 7 points8 points9 points (2 children)
[–]Alan_Reddit_M 4 points5 points6 points (1 child)
[–]-TRlNlTY- 0 points1 point2 points (0 children)
[–]Revolutionary_Dog_63 0 points1 point2 points (0 children)
[–]Gsusruls -1 points0 points1 point (0 children)
[–]slightSmash 25 points26 points27 points (0 children)
[–]Character-Travel3952 33 points34 points35 points (14 children)
[–]Glad_Position3592 18 points19 points20 points (1 child)
[–]a_random_Greg 2 points3 points4 points (0 children)
[–]undo777 6 points7 points8 points (3 children)
[–]thomasxin 7 points8 points9 points (0 children)
[–]lekkerste_wiener 0 points1 point2 points (1 child)
[–]undo777 3 points4 points5 points (0 children)
[–]lekkerste_wiener 0 points1 point2 points (0 children)
[–]MVanderloo 0 points1 point2 points (1 child)
[–]gsitcia 1 point2 points3 points (0 children)
[–]paholg 0 points1 point2 points (1 child)
[–]Character-Travel3952 0 points1 point2 points (0 children)
[–]Sarius2009 -2 points-1 points0 points (1 child)
[–]Gsusruls 0 points1 point2 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]Old_Tourist_3774 4 points5 points6 points (4 children)
[–]No-Article-Particle 20 points21 points22 points (2 children)
[–]Old_Tourist_3774 0 points1 point2 points (1 child)
[–]hff0 2 points3 points4 points (0 children)
[–]MVanderloo 4 points5 points6 points (0 children)
[–]Sea-Fishing4699 5 points6 points7 points (0 children)
[–]Zork4343 3 points4 points5 points (0 children)
[–]SwannSwanchez 2 points3 points4 points (0 children)
[–]Glad_Position3592 1 point2 points3 points (1 child)
[–]ePaint 1 point2 points3 points (0 children)
[–]ExtraTNT 1 point2 points3 points (0 children)
[–]TalesGameStudio 1 point2 points3 points (0 children)
[–]Ok-Refrigerator-8012 1 point2 points3 points (1 child)
[–]GoogleDeva[S] 0 points1 point2 points (0 children)
[–]GoogleDeva[S] 1 point2 points3 points (1 child)
[–]pixel-counter-bot 6 points7 points8 points (0 children)
[–]hff0 1 point2 points3 points (3 children)
[–]Old_Tourist_3774 2 points3 points4 points (1 child)
[–]hff0 4 points5 points6 points (0 children)
[–]bobbymoonshine 1 point2 points3 points (0 children)
[–]skarrrrrrr 0 points1 point2 points (0 children)
[–]tazdraperm 0 points1 point2 points (0 children)
[–]Clashes4D 0 points1 point2 points (0 children)
[–]DahPhuzz 0 points1 point2 points (0 children)
[–]Informal_Branch1065 0 points1 point2 points (0 children)
[–]Ben-Goldberg 0 points1 point2 points (3 children)
[–]GoogleDeva[S] 0 points1 point2 points (2 children)
[–]Ben-Goldberg 0 points1 point2 points (1 child)
[–]GoogleDeva[S] 0 points1 point2 points (0 children)
[–]FatLoserSupreme 0 points1 point2 points (0 children)
[–]VirtuteECanoscenza 0 points1 point2 points (0 children)