293
top 38 comments
sorted by: hot top controversial new old

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

[-] lugal@sopuli.xyz 38 points 1 year ago

Also 0 and empty strings

[-] bleistift2@feddit.de 31 points 1 year ago

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

[-] joyjoy@lemm.ee 24 points 1 year ago* (last edited 1 year ago)

results = list(filter(None, results))

[-] LostXOR@kbin.social 4 points 1 year ago

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

[-] naught@sh.itjust.works 22 points 1 year ago
[-] LostXOR@kbin.social 3 points 1 year ago

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

[-] AVincentInSpace@pawb.social 9 points 1 year ago

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

[-] drew_belloc@programming.dev 43 points 1 year ago
[-] bleistift2@feddit.de 39 points 1 year ago

Meanwhile in Java land:

Map map = new Map()
[-] Korne127@lemmy.world 24 points 1 year ago

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

[-] bleistift2@feddit.de 10 points 1 year ago
[-] metaStatic@kbin.social 8 points 1 year ago

Should have used memory safe rust.

nailed it

[-] frezik@midwest.social 34 points 1 year ago

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

[-] stebo02@sopuli.xyz 26 points 1 year ago

I'd say this is pretty readable

[-] frezik@midwest.social 8 points 1 year ago

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

[-] stebo02@sopuli.xyz 4 points 1 year ago

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

[-] frezik@midwest.social 3 points 1 year ago

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

[-] stebo02@sopuli.xyz 5 points 1 year ago

or you're doing this a million times

[-] addie@feddit.uk 16 points 1 year ago

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.

[-] lunarul@lemmy.world 12 points 1 year ago

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

[-] AngrilyEatingMuffins@kbin.social 26 points 1 year ago

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@lemmy.dbzer0.com 35 points 1 year ago

Has someone finally just given ChatGPT a Lemmy account?

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

[-] nautilus@lemmy.dbzer0.com 4 points 11 months ago

Good lord, it’s gained sentience

[-] riodoro1@lemmy.world 24 points 1 year ago

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@lemmy.ca 15 points 11 months ago* (last edited 11 months ago)

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

new_results = []
for result in results:
    if result:
        new_results.append(result)
results = new_results

or else

for result in results:
    if not result:
        results.remove(result)

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.

[-] Gork@lemm.ee 18 points 1 year ago

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

[-] mvirts@lemmy.world 12 points 1 year ago

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

[-] itslilith@lemmy.blahaj.zone 12 points 1 year ago* (last edited 1 year ago)

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

results = list(filter(bool, results))
[-] kakes@sh.itjust.works 12 points 1 year ago

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

[-] lugal@sopuli.xyz 4 points 1 year ago

Like None, 0, "", ...

[-] itslilith@lemmy.blahaj.zone 2 points 1 year ago

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

[-] Nevoic@lemm.ee 9 points 11 months ago* (last edited 11 months ago)

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:

new_results = results.filter(x -> x)

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

new_results = results.filter(_)

Of course in actual Python you'd instead see:

new_results = list(filter(lambda x: x, results))

which is arguably worse than

new_results = [x for x in results if x]
[-] riquisimo@lemmy.world 5 points 1 year ago
[-] pomodoro_longbreak@sh.itjust.works 4 points 11 months ago

hey if you can think of a better way

[-] Toes@ani.social 3 points 1 year ago

Looks like something I'd write while high

[-] catfish@programming.dev 2 points 1 year ago
this post was submitted on 22 Nov 2023
293 points (100.0% liked)

196

16501 readers
2037 users here now

Be sure to follow the rule before you head out.

Rule: You must post before you leave.

^other^ ^rules^

founded 1 year ago
MODERATORS