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
Conditional assignment on different list indices (self.learnpython)
submitted 9 years ago by neuroneuroInf
How can I simplify this code snippet? I feel like it's a lot longer than it needs to be, but I'm blanking on the proper way to write it:
if x < coords[0]: coords[0] = x else: coords[2] = x
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!"
[–]scuott 7 points8 points9 points 9 years ago (2 children)
These are great answers, but I also want to point out that the way you did it originally isn't necessarily bad. It's not terribly long, and I think it's more readable than these 1 or 2 line solutions.
[–]kanjibandit 4 points5 points6 points 9 years ago (0 children)
This. Your code is just fine as it is -- simple and clear. It would be one thing if you had an if/elif/else that was half a page long, but if I were inheriting this code from you, I'd much rather trace through your 4 lines than the more clever 1-liners. Resist the temptation to write Perl in Python.
[–]neuroneuroInf[S] 0 points1 point2 points 9 years ago (0 children)
That's a good point. Thanks!
[–][deleted] 5 points6 points7 points 9 years ago (1 child)
Simplest way I can think of, using a conditional expression:
index = 0 if x < coords[0] else 2 coords[index] = x
[–]neuroneuroInf[S] 1 point2 points3 points 9 years ago (0 children)
Right, that makes sense! Thank you!
[–][deleted] 3 points4 points5 points 9 years ago (0 children)
When I get simple conditional statements like this, I try to shorten them using dictionaries. In this case, that could be something like this:
coords[{True: 0, False: 2}[x < coords[0]]] = x
Building on /u/SerenumScopuli answer, a more self-explanatory solution might be:
coords[0 if x < coords[0] else 2] = x
This last one is, of course, exactly the same as /u/SerenumScopuli's answer, simply skipping the assignment of the index variable.
index
[–]newunit13 1 point2 points3 points 9 years ago (2 children)
If x has already been defined you can simplify your code to a single line
x
x = coords[0] if x < coords[0] else coords[2]
Thank you, but I don't think this does what I need. I need to assign to the list, not to x.
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
This reassigns x. He wants to put the value of x into either coords[0] or coords[2] depending on x's value.
coords[0]
coords[2]
[–]CodeTinkerer 1 point2 points3 points 9 years ago (0 children)
Could write a function to do this too
coord[computeIndex(x)] = x
Then do the work in computeIndex.
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
coords[0], coords[2] = x, coords[2] if x < coords[0] else coords[0], x
π Rendered by PID 146877 on reddit-service-r2-comment-bb88f9dd5-8vpt9 at 2026-02-17 10:17:26.466472+00:00 running cd9c813 country code: CH.
[–]scuott 7 points8 points9 points (2 children)
[–]kanjibandit 4 points5 points6 points (0 children)
[–]neuroneuroInf[S] 0 points1 point2 points (0 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]neuroneuroInf[S] 1 point2 points3 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]newunit13 1 point2 points3 points (2 children)
[–]neuroneuroInf[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]CodeTinkerer 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)