Comments on Tips for golfing in Python
Parent
Tips for golfing in Python
If you have any tips for golfing in Python, add them as answers to this post.
Use the unpacking `` instead o …
5y ago
If you print some string `s` w …
5y ago
Reorder expressions in order t …
5y ago
Combine conditionals Python …
5y ago
Replace `n+1` with `-n` and `n …
5y ago
Replace if/else expressions by …
5y ago
Use `a+=[b]` instead of `a.app …
5y ago
` == and` If you want to ch …
5y ago
Combine loops Suppose you h …
5y ago
Replace `range()` if $n …
5y ago
Renaming functions A funny, …
1y ago
`import` You can import som …
5y ago
Replace print loops by list un …
5y ago
Replace `for` loops with strin …
5y ago
If you have a loop or `if`, yo …
5y ago
Assignment expressions (Python …
1y ago
Assign `float`s while leaving …
5y ago
Use `lambda`s instead of funct …
5y ago
`| == or` This bitwise oper …
5y ago
Post
Renaming functions
A funny, cool, and useful trick. If you have to write a single function multiple times (usually range() on for loops), then you can simply assign a variable by the function's name, no parentheses. An example assignment is:
r=range
Thing is, this can only apply to save bytes if the function name is long enough to save more from the times it was used than the cost of assigning it to a variable. If not even twice, it's unnecessary. There're rarely any answers using this format for this exact reason. There aren't a lot of times where you'd need to keep adding the same function on the program when it comes to golfing.

0 comment threads