Nevoic,

Python’s disdain for the industry standard is wild. Every other language made in the last 20 years has proper filtering that doesn’t require collecting the results back into a list after filtering like Java (granted it’s even more verbose in Java but that’s a low bar).

If Python had modern lambdas and filter was written in an inclusion or parametric polymorphic way, then you could write:


<span style="color:#323232;">new_results = results.filter(x -> x)
</span>

Many languages have shorthands to refer to variables too, so it wouldn’t be impossible to see:


<span style="color:#323232;">new_results = results.filter(_)
</span>

Of course in actual Python you’d instead see:


<span style="color:#323232;">new_results = list(filter(lambda x: x, results))
</span>

which is arguably worse than


<span style="color:#323232;">new_results = [x for x in results if x]
</span>
pomodoro_longbreak,
@pomodoro_longbreak@sh.itjust.works avatar

hey if you can think of a better way

riodoro1,

Python has great list comprehension. Too bad it’s incomprehensible to humans.

But seriously whoever writes those cool oneliners is a shitty programmer. Life is not code golf, fuck off with your smart ass.

Bonsoir, (edited )

What would be the alternative? (assuming that you want to do the loop yourself)


<span style="color:#323232;">new_results </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">[]
</span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">result </span><span style="font-weight:bold;color:#a71d5d;">in </span><span style="color:#323232;">results:
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">result:
</span><span style="color:#323232;">        new_results.append(result)
</span><span style="color:#323232;">results </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">new_results
</span>

or else


<span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">result </span><span style="font-weight:bold;color:#a71d5d;">in </span><span style="color:#323232;">results:
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">if not </span><span style="color:#323232;">result:
</span><span style="color:#323232;">        results.remove(result)
</span>

which doesn’t do the exact same thing.
Honestly, this list comprehension is much faster to read and quite easy to understand.
I think we could rename the “result” variable “x” or “res” and it would be less confusing though.

mvirts,

I’ve written this more times than I can remember 😹 who needs filter anyway? Gotta use up all this ram.

frezik,

People used to argue that Python was incredibly readable. Then I started seeing shit like this.

lunarul,

I don’t know python, but it’s clear what that line does

addie,
@addie@feddit.uk avatar

I think that Python has a bit of a ‘Microsoft Word’ thing on the go. You know how your own docs are completely editable and print fine, but everyone else’s are a complete fucking disaster and pressing a single key will screw up the formatting of the whole document? Your own Python code is full of sensible idioms and pragmatic naming conventions, but everyone else’s was plainly written while on mushrooms.

stebo02,
@stebo02@sopuli.xyz avatar

I’d say this is pretty readable

frezik,

It jams far too much on one line. Break it up. It’s a mistake I see a lot on Python.

stebo02,
@stebo02@sopuli.xyz avatar

but if you do it as a for loop it would be slower

frezik,

Unless you’re looping over more than a million elements, that’s a poor excuse.

stebo02,
@stebo02@sopuli.xyz avatar

or you’re doing this a million times

Gork,

[ L I S T C O M P R E H E N T I O N ]

catfish,

Checks out

riquisimo,

True.

Synthead,

Ruby has a method for this :)


<span style="color:#323232;">[1] pry(main)> vars = ["one", "two", nil, "three"]
</span><span style="color:#323232;">=> ["one", "two", nil, "three"]
</span><span style="color:#323232;">[2] pry(main)> vars.compact
</span><span style="color:#323232;">=> ["one", "two", "three"]
</span>

In Ruby, 0 and “” is truthy, so this would be a little different than the Python interpretation. You could filter with , but you’d typically write your code around this instead.

CoderKat,

In Ruby, 0 and “” is truthy,

What the fuck?

Synthead,

Yup :) Everything in Ruby inherits Object, too. It’s a really neat language, and when you get accustomed to it, you might wonder why some other languages aren’t written like it.

For the 0 value being truthy, consider that Ruby is a dynamic language. Imagine if you asked a user how many motorcycles they own. If they answer, you’ll have an Integer. If they don’t, you’ll have nil, from NilClass. 0 is just as valid of an answer as 2, so you can do something like this:


<span style="color:#323232;">raise NoResponse unless motorcycles
</span><span style="color:#323232;">
</span><span style="color:#323232;">save_motorcycles(motorcycles)
</span>
diemartin,

Lua is the same. Only false and nil are “falsey”.

I remember I fixed a bug in a mod for Minetest that assumed 0 was false and caused an infinite loop.

DarkenLM,

And people bash Javascript as if it was the devil when thinks like this exist on other languages.

calcopiritus,

Python also has about 9000 alternatives that are better than this.

Allowing anything other than variables, number literals, and ‘:’ inside list indices was a mistake.

AngrilyEatingMuffins,

The image you've uploaded is a humorous take on a programming practice common among Python developers. It shows a list comprehension, which is a concise way to create lists in Python. The joke is that nobody prompted the Python programmers to use a complex or sophisticated feature, yet they are using it anyway, which implies that Python programmers tend to use list comprehensions frequently and perhaps even when they are not strictly necessary. List comprehensions are a popular feature in Python because they can make the code more readable and expressive, and this meme plays on the idea that Python programmers might be eager to use them at every opportunity.

nautilus,

Has someone finally just given ChatGPT a Lemmy account?

AngrilyEatingMuffins,

i think there actually is one i just copy pasted for the meeeem

nautilus,

Good lord, it’s gained sentience

bleistift2,

Meanwhile in Java land:


<span style="color:#323232;">Map map = new Map()
</span>
Korne127,
@Korne127@lemmy.world avatar

Map is an interface though, you’d have to use HashMap 😅

bleistift2,

I don’t visit often…

metaStatic,

Should have used memory safe rust.

nailed it

InEnduringGrowStrong,
@InEnduringGrowStrong@sh.itjust.works avatar

I mean this would remove False and None from a list though.

lugal,

Also 0 and empty strings

bleistift2,

And empty lists, tuples, dictionaries, sets, and strings

LostXOR,

results = [result for result in results if result != None]

naught,

Should be is not None (:

LostXOR,

You're right, though IIRC there's no functional difference when comparing to None (other than speed).

AVincentInSpace,

Yes there is. One invokes __ne__ on the left hand side, the other performs an identity comparison.

joyjoy,

results = list(filter(None, results))

itslilith,
@itslilith@lemmy.blahaj.zone avatar

It does! it takes a list (or other iterator) and filters out all values that are cast to boolean True. The same could be archived with


<span style="color:#323232;">results </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">list</span><span style="color:#323232;">(</span><span style="color:#62a35c;">filter</span><span style="color:#323232;">(</span><span style="color:#0086b3;">bool</span><span style="color:#323232;">, results))
</span>
kakes,

It would filter out values that cast to False, no?

lugal,

Like None, 0, “”, …

itslilith,
@itslilith@lemmy.blahaj.zone avatar

i wasn’t sure how to phrase it, it keeps all values that cast to True, and discards all that cast to False

Toes,

Looks like something I’d write while high

drew_belloc,
@drew_belloc@programming.dev avatar
  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • [email protected]
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • oklahoma
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • SuperSentai
  • feritale
  • KamenRider
  • All magazines