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 …
3y ago
If you print some string `s` w …
3y ago
Reorder expressions in order t …
3y ago
Combine conditionals Python …
3y ago
Replace `n+1` with `-n` and `n …
3y ago
Replace if/else expressions by …
3y ago
Use `a+=[b]` instead of `a.app …
3y ago
` == and` If you want to ch …
3y ago
Combine loops Suppose you h …
3y ago
Replace `range()` if $n …
3y ago
Renaming functions A funny, …
16d ago
`import` You can import som …
3y ago
Replace print loops by list un …
3y ago
Replace `for` loops with strin …
3y ago
If you have a loop or `if`, yo …
3y ago
Assignment expressions (Python …
24d ago
Assign `float`s while leaving …
3y ago
Use `lambda`s instead of funct …
3y ago
`| == or` This bitwise oper …
3y ago
Post
Combine loops
Suppose you have a for
loop in another, maybe a couple of times, and nothing else inside of the outer for
loops (except for the first for
loop since anything outside won't be involved at all). It's probably a range()
problem again.
If you're doing something like this:
x=10;y=15
for i in range(x):
for j in range(y):
print(i,"-",j)
You can simply set the loop as one:
x=10;y=15
for i in range(x*y):print(i//y,"-",i%y)
Golfing 16 bytes in total. You can do this with more loops, but this is what's functional for now.
0 comment threads