Timeline for Tips for golfing in Python
Current License: CC BY-SA 4.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| S Aug 16 at 1:01 | history | suggested | Mr. Lance E Sloan | CC BY-SA 4.0 |
`a<b` was a bad example condition for this tip. As comments mentioned, this whole thing could be simplified as `min(a,b)`. I've changed the condition to use different values, to demonstrate a more general example.
|
| Aug 15 at 18:31 | comment | added | Mr. Lance E Sloan | @Acccumulation is right. I thought this was a poor example for this tip. I've changed the condition to use different values, to demonstrate a more general example. | |
| Aug 15 at 18:29 | review | Suggested edits | |||
| S Aug 16 at 1:01 | |||||
| Aug 30, 2023 at 19:31 | comment | added | kr8gz |
If you swap the even and odd you can make it even shorter with this monstrosity: ["eovdedn"[i%2::2]for i in range(10)]
|
|
| Jun 22, 2022 at 12:54 | comment | added | 0x263A |
Also useful inside of list comprehensions e.g. [("odd","even")[i%2] for i in range(10)] compared to ["odd"if i%2 else"even" for i in range(10)]
|
|
| S Jun 17, 2022 at 18:51 | history | suggested | 0x263A | CC BY-SA 4.0 |
Language identifiers
|
| Jun 17, 2022 at 15:33 | review | Suggested edits | |||
| S Jun 17, 2022 at 18:51 | |||||
| Apr 17, 2018 at 20:16 | comment | added | Acccumulation | Of course, in this particular case, it can be golfed further to 'return min(a,b)' | |
| Feb 16, 2016 at 12:24 | comment | added | Ogaday |
Be careful of using this to do recursion, ie. f = lambda a:(a, f(a-1))[a>1] because this will evaluate the options before the conditional, unlike f = lambda a: f(a-1) if a>1 else a, which only executes the recursive f(a-1) if the condition a>1 evaluates to True.
|
|
| Feb 20, 2015 at 2:35 | history | wiki removed | Doorknob | ||
| Jul 25, 2014 at 13:22 | history | edited | user80551 | CC BY-SA 3.0 |
added 5 characters in body
|
| Mar 20, 2014 at 9:06 | comment | added | Chase Ries | @user2357112 But they make you look so much cooler when you use them. :] | |
| Mar 20, 2014 at 7:07 | comment | added | user2357112 | Lambdas are way longer than a conditional expression. | |
| Nov 7, 2013 at 18:14 | review | Suggested edits | |||
| Nov 7, 2013 at 18:27 | |||||
| Apr 24, 2013 at 11:01 | comment | added | kgadek |
@marinus, they are not equal: just consider P and A or B for any A that gives bool(A)=False. But (P and [A] or [B])[0] will do the job. See diveintopython.net/power_of_introspection/and_or.html for reference.
|
|
| Dec 20, 2011 at 14:00 | history | edited | hallvabo | CC BY-SA 3.0 |
shortened first example by removing newline
|
| May 7, 2011 at 23:33 | comment | added | Ming-Tang |
(lambda(): b, lambda(): a)[a < b]() make your own short-circuit with lambdas
|
|
| May 3, 2011 at 11:34 | comment | added | marinus |
These aren't exactly the same. The first one evaluates only the expression that is returned while the second one always evaluates them both. These ones do short-circuit: a if a<b else b and a<b and a or b
|
|
| Feb 3, 2011 at 13:15 | history | edited | gnibbler | CC BY-SA 2.5 |
deleted 1 characters in body
|
| Jan 28, 2011 at 0:12 | history | answered | moinudin | CC BY-SA 2.5 |