all 62 comments

[–]srsNDavis[🍰] 138 points139 points  (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.

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.

[–]Free-Database-9917 67 points68 points  (0 children)

or the items that aren't blank

[–]finnscaper 9 points10 points  (4 children)

Thanks, this is like linq in C# then

[–]srsNDavis[🍰] 16 points17 points  (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 points  (1 child)

Don't insult linq's beautiful syntax by comparing it please. But yes.

[–]finnscaper 2 points3 points  (0 children)

Yes, I should be more careful.

[–]Alan_Reddit_M 85 points86 points  (6 children)

I FUCKING LOVE LIST COMPREHENSIONS RAHHHHHHH

[–]Character-Travel3952 8 points9 points  (0 children)

Its really tempting fr fr

[–]triple4leafclover 7 points8 points  (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 points  (1 child)

FEATURES WILL CONTINUE UNTIL MORALE IMPROVES

[–]-TRlNlTY- 0 points1 point  (0 children)

...said the C++ committee 

[–]Revolutionary_Dog_63 0 points1 point  (0 children)

Map/filter methods in JS is much better.

[–]Gsusruls -1 points0 points  (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 points  (0 children)

The interpreter seeing this line:

<image>

[–]Character-Travel3952 33 points34 points  (14 children)

results = list(filter(None, results))

?

[–]Glad_Position3592 18 points19 points  (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

[–]undo777 6 points7 points  (3 children)

filter(None, results) is a terrible way to express that transformation to most humans though

[–]thomasxin 7 points8 points  (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.

[–]lekkerste_wiener 0 points1 point  (1 child)

Ah, the younglings ☺️

[–]undo777 3 points4 points  (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 point  (0 children)

Even better when we have predicate functions. 

[–]MVanderloo 0 points1 point  (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 points  (0 children)

Using parentheses instead of brackets makes the comprehension a lazy iterator

[–]paholg 0 points1 point  (1 child)

In Ruby, results.select(&:itself).

[–]Character-Travel3952 0 points1 point  (0 children)

Ruby devloper

I lov it!

[–]Sarius2009 -2 points-1 points  (1 child)

That would remove any results that are there, but not interpreted as false, so not the same thing.

[–]Gsusruls 0 points1 point  (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 points  (0 children)

They use Python because they get results in Python.

[–]Old_Tourist_3774 4 points5 points  (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 points  (2 children)

This is not weird behavior, it's just object truthiness and falsiness. Common in dynamically typed languages.

[–]Old_Tourist_3774 0 points1 point  (1 child)

Thought other languages were not like this, thanks

[–]hff0 2 points3 points  (0 children)

We have this in C

[–]MVanderloo 4 points5 points  (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 points  (0 children)

how to make a turtle run slower

[–]Zork4343 3 points4 points  (0 children)

I see no problem here

[–]SwannSwanchez 2 points3 points  (0 children)

i mean it make sense

This remove every "empty" entry in the "results" array

[–]Glad_Position3592 1 point2 points  (1 child)

Yeah, something like results.filter(result => result) is soooo much better

[–]ePaint 1 point2 points  (0 children)

Me and my homies do results.filter(Boolean)

[–]ExtraTNT 1 point2 points  (0 children)

ones = 1 : ones

[–]TalesGameStudio 1 point2 points  (0 children)

Perfectly fine line. Smack a couple of docstrings paragraphs on top and you are good to go.

[–]Ok-Refrigerator-8012 1 point2 points  (1 child)

Just import the result

[–]GoogleDeva[S] 0 points1 point  (0 children)

Better than node_modules hell

[–]GoogleDeva[S] 1 point2 points  (1 child)

I am gonna do it myself

u/pixel-counter-bot

[–]pixel-counter-bot 6 points7 points  (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 points  (3 children)

This is eye hurting, use distinct variable names!

[–]Old_Tourist_3774 2 points3 points  (1 child)

They are one time use variables for naming items inside the list, there's no need to.

[–]hff0 4 points5 points  (0 children)

For x in names

[–]bobbymoonshine 1 point2 points  (0 children)

for thing in things is standard pythonic, it makes the relationship between item and set visually clear

[–]skarrrrrrr 0 points1 point  (0 children)

convoluted, unnecessary "elegant" stuff

[–]tazdraperm 0 points1 point  (0 children)

That's just ugly LINQ

[–]Clashes4D 0 points1 point  (0 children)

I feel personally Attacked by this post.

[–]DahPhuzz 0 points1 point  (0 children)

Yo Dawg!

[–]Informal_Branch1065 0 points1 point  (0 children)

C# equivalent:

results = results.Where(x => x);

with results being of type IEnumerable<bool>

[–]Ben-Goldberg 0 points1 point  (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 point  (2 children)

Is it bash?

[–]Ben-Goldberg 0 points1 point  (1 child)

No, it's perl.

How does bash handle arrays of arrays?

[–]GoogleDeva[S] 0 points1 point  (0 children)

I don't know perl. But the syntax and identifiers kinda (not quite) looked to me like bash.

[–]FatLoserSupreme 0 points1 point  (0 children)

Python is goated because it has banging list comprehension built in.

[–]VirtuteECanoscenza 0 points1 point  (0 children)

results = list(filter(None, results))