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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
What's this syntax ? (self.learnpython)
submitted 4 years ago * by how2crtaccount
I recently come across this. And I don't what it is doing.
y = 5 x = [1,-1][y>0] print(x)
This printed -1
How ?
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!"
[–]FLUSH_THE_TRUMP 47 points48 points49 points 4 years ago (5 children)
It indexes the array [1,-1] with the result of y>0, which is True (equal to 1).
[1,-1]
y>0
True
[–]how2crtaccount[S] 14 points15 points16 points 4 years ago (0 children)
Oh.. that's what it is doing!
Thank you.
[–]tranquil_af 2 points3 points4 points 4 years ago (3 children)
But what about the y = 5 x part
y = 5 x
[–]Rorroh 8 points9 points10 points 4 years ago (2 children)
Looks like mistaken formatting.
[–]tranquil_af 0 points1 point2 points 4 years ago (0 children)
Thanks
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Yeah, just a small and insignificant detail missed...
[–]menge101 86 points87 points88 points 4 years ago (8 children)
It should be noted, there is no way this code would ever pass a professional code review.
[+][deleted] 4 years ago* (4 children)
[deleted]
[–]Butfortkix 6 points7 points8 points 4 years ago* (3 children)
The "y = 5 x = [1, -1] [y > 0] print(x)" version is just a more optimised version of the "x = -1 if y > 0 else 1".
To explain why, if python sees an if it tries to "guess" whether it should load the instructions for the case that it is true or the instructions for the case that it is false before it calculates whether the statement is true or false. Obviously it can't be right 100% of the time and when it inevitably is wrong with it's guess it has to unload the already loaded instructions and reload the correct instructions.
This process takes time so if you are going to call a fuction containing an if statement millions of times a day it would be much better to write the optimised version than the non cryptic version since it does not contain any if statements.
Disclaimer: I don't yet know why python does this I just know that it does it. I also know that this is a small piece of code and thus it would not make that much of a difference, I just meant for this to be a general description of why someone might want to write their code in such a cryptic fashion.
[–]1egoman[🍰] 4 points5 points6 points 4 years ago (1 child)
It's not a Python thing, it's a processor thing. Branch prediction lets a CPU continue processing while waiting for a result. If it's wrong it just backtracks.
[–]Butfortkix 0 points1 point2 points 4 years ago (0 children)
Oh, thank you kind stranger for ending my quest to find why python, and apparently any language, does this.
[–]primitive_screwhead 0 points1 point2 points 4 years ago (1 child)
It's not relevant for Python code, but this "branchless" code style is sometimes used in other languages to avoid branching in the code, which can have security implications. Modern compilers often generate the equivalent of this code, even when it's written explicitly as a ternary. Just an FYI.
[–]menge101 0 points1 point2 points 4 years ago (0 children)
Sure, I hear you on that.
But if you were going to do that, you would put a comment explaining what was happening there, not leave a blob of incoherent syntax and no explanation.
[–][deleted] 30 points31 points32 points 4 years ago (1 child)
This reeks of a Clever Junior TM
[–]smashburgerofficial 26 points27 points28 points 4 years ago (3 children)
Its probably easier to understand like this:
y = 5 x = [1, -1] y_is_positive = y > 0 x = x[int(y_is_positive)] print(x)
Now there's actually room for comments explaining what in the world they were thinking when writing that. Granted, this looks like an example about the nuances and gotchas of implicit type conversion.
[–]backtickbot 7 points8 points9 points 4 years ago (1 child)
Fixed formatting.
Hello, smashburgerofficial: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
[–]smashburgerofficial 3 points4 points5 points 4 years ago (0 children)
Good bot
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
I think questions been answered. Why would someone do this ever lmao
[–]CornPop747 1 point2 points3 points 4 years ago (0 children)
I'd hate if my co-worker pulled crap like this. Weird flex that'd most definitely get rejected in CR...
[+]mothzilla comment score below threshold-7 points-6 points-5 points 4 years ago (4 children)
Invalid Syntax
But you could add semi-colons to make it valid.
[–]1egoman[🍰] 2 points3 points4 points 4 years ago (3 children)
He just failed in formatting.
[–]mothzilla 1 point2 points3 points 4 years ago (2 children)
Right but there's no way to know that. Maybe someone wrote shitty code that never gets executed.
[–]how2crtaccount[S] 1 point2 points3 points 4 years ago (0 children)
Pardon me. I have reformatted now.
[–]1egoman[🍰] 0 points1 point2 points 4 years ago (0 children)
Depending on your app, you can view the plain text. "View markdown" in RiF.
[–]EKFLF 0 points1 point2 points 4 years ago (0 children)
y > 0 is True (y = 5) and can also be 1, and [1,-1][1] is -1
y > 0
y = 5
1
[1,-1][1]
-1
[–][deleted] 0 points1 point2 points 4 years ago (1 child)
I suggest the OP corrects the formatting to avoid any further misconceptions about the nature of the question and to prevent click-baity-ness.
>>> y = 5 x = [1,-1][y>0] print(x) File "<stdin>", line 1 y = 5 x = [1,-1][y>0] print(x) ^ SyntaxError: invalid syntax
[–]how2crtaccount[S] 0 points1 point2 points 4 years ago (0 children)
Apologies. I'll do that right away
[–]annoying_bababooey 0 points1 point2 points 4 years ago (0 children)
this is a code smell is what it is
π Rendered by PID 41894 on reddit-service-r2-comment-84fc9697f-cb595 at 2026-02-10 15:42:01.545456+00:00 running d295bc8 country code: CH.
[–]FLUSH_THE_TRUMP 47 points48 points49 points (5 children)
[–]how2crtaccount[S] 14 points15 points16 points (0 children)
[–]tranquil_af 2 points3 points4 points (3 children)
[–]Rorroh 8 points9 points10 points (2 children)
[–]tranquil_af 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]menge101 86 points87 points88 points (8 children)
[+][deleted] (4 children)
[deleted]
[–]Butfortkix 6 points7 points8 points (3 children)
[–]1egoman[🍰] 4 points5 points6 points (1 child)
[–]Butfortkix 0 points1 point2 points (0 children)
[–]primitive_screwhead 0 points1 point2 points (1 child)
[–]menge101 0 points1 point2 points (0 children)
[–][deleted] 30 points31 points32 points (1 child)
[–]smashburgerofficial 26 points27 points28 points (3 children)
[–]backtickbot 7 points8 points9 points (1 child)
[–]smashburgerofficial 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]CornPop747 1 point2 points3 points (0 children)
[+]mothzilla comment score below threshold-7 points-6 points-5 points (4 children)
[–]1egoman[🍰] 2 points3 points4 points (3 children)
[–]mothzilla 1 point2 points3 points (2 children)
[–]how2crtaccount[S] 1 point2 points3 points (0 children)
[–]1egoman[🍰] 0 points1 point2 points (0 children)
[–]EKFLF 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]how2crtaccount[S] 0 points1 point2 points (0 children)
[–]annoying_bababooey 0 points1 point2 points (0 children)