What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is not an answer).
Please post one tip per answer.
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.
Sign up to join this communityWhat general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is not an answer).
Please post one tip per answer.
Python tokens only need to separated by a space for
In all other cases, the space can be omitted (with a few exceptions). Here's a table.
L D S
+-----
L|s s n
D|n - n
S|n n n
First token is row, second token is column
L: Letter
D: Digit
S: Symbol
s: space
n: no space
-: never happens (except multidigit numbers)
Letter followed by letter: Space
not b
for x in l:
lambda x:
def f(s):
x in b"abc"
Letter followed by digit: Space
x or 3
while 2<x:
Letter followed by symbol: No space
c<d
if~x:
x and-y
lambda(a,b):
print"yes"
return[x,y,z]
Digit followed by letter: No space
x+1if x>=0else 2
0in l
(Some versions of Python 2 will fail on a digit followed by else
or or
.)
Digit followed by digit: Never occurs
Consecutive digits make a multidigit number. I am not aware of any situation where two digits would be separated by a space.
Digit followed by symbol: No space
3<x
12+n
l=0,1,2
A space is needed for 1 .__add__
and other built-ins of integers, since otherwise the 1.
is parsed as a float.
Symbol followed by letter: No space
~m
2876<<x&1
"()"in s
Symbol followed by digit: No space
-1
x!=2
Symbol followed by symbol: No space
x*(a+b)%~-y
t**=.5
{1:2,3:4}.get()
"% 10s"%"|"
e
is expected to be a float literal, so something like 1else
wouldn't work for versions of python that support exponents in the literal. Similarly, as 0o
is the prefix of an octal literal, o
can follow any digit but 0
. For the complete lexical rules, refer to docs.python.org/2/reference/lexical_analysis.html
\$\endgroup\$
– xsot
Aug 1 '16 at 5:20
assert True == 1
assert False == 0
assert 2 * True == 2
assert 3 * False == 0
assert (2>1)+(1<2) == 2
If you have a statement like [a,a+x][c]
(where c
is some boolean expression), you can do a+x*c
instead and save a few bytes. Doing arithmetic with booleans can save you lots of bytes!
If you're drawing, for colors, instead of typing:
'#000'
for black you can just use 0
(no apostrophes)
'#fff'
for white you can simply use ~0
(no apostrophes)
'#f00'
for red you can just use 'red'
Example of white being used with ~0
from PIL.ImageDraw import*
i=Image.new('RGB',(25,18),'#d72828')
Draw(i).rectangle((1,1,23,16),'#0048e0',~0)
i.show()
255
is even shorter than 'red'
. Some more ideas: ~255
is '#0ff'
(cyan). 1<<7
is #800000
(half-brightness red); similarly 1<<15
and 1<<23
are half-brightness green and blue.
\$\endgroup\$
– Lynn
Jan 3 '18 at 19:10
You can generate the contiguous substrings of a string s
with a recursive function (Python 3 for [*s]
).
f=lambda s:[*s]and[s]+f(s[1:])+f(s[:-1])
This will repeat substrings multiple times, but can be made a set to avoid repeats.
f=lambda s:{*s}and{s}|f(s[1:])|f(s[:-1])
To assign to a tuple, don't use parentheses. For example, a=1,2,3
assigns a
to the tuple (1, 2, 3)
. b=7,
assigns b
to the tuple (7,)
. This works in both Python 2 and Python 3.
Python 2 and 3 differences
Recent challange pushed me to search for differences in two major versions of python. More precisely - same code, that returns different results in different versions. This might be helpful in other polyglot challenges.
1) Strings and bytes comparisson
'' == b''
'' != b''
2) Rounding (Luis Mendo answer)
round(1*0.5) = 1.0
round(1*0.5) = 0
3) Division (Jonathan Allan answer)
10/11 = 0
10/11 = 0.9090909090909091
4) Suggestions?
a%b==a
if b
has a constant signFor two expressions a
and b
, where each one results in an int
(or long
in Python 2) or float
, you can replace these:
a%b==a
a==a%b
with these, if b
is positive:
0<=a<b
b>a>=0
or these, if b
is negative:
b<a<=0
0>=a>b
I'm presenting two expressions for each case because sometimes you may want to use one over the other to eliminate a space to separate expression b
from an adjacent token. They both have the same precedence, so you're not usually going to need to surround the second expression with ()
if you don't need to do so to the first one.
This is useful if expression a
is more than 1 byte long or b
is negative, because it removes one occurrence of a
from the expression. If \$a,b\$ are the lengths of expressions a
and b
respectively, and \$l\$ is the length of the original expression, the resulting expression will be \$l-a+1\$ bytes long. Note that this method is always going to be shorter than assigning expression a
to a separate variable.
For example,
(a+b)%c==a+b
can be replaced with
0<=a+b<c
for a total saving of 4 bytes.
Let's define the operator \$x\mathbin\%y\$ for \$x,y\in\mathbb Q\$.
Every rational number \$a\$ can be represented as \$a=bq+r\$, where \$q\in\mathbb Z,0\le r<|b|\$. Therefore, we can define an operator \$a\mathbin\%b\$, where the result has the same sign as \$b\$:
$$a=bq+r,q\in\mathbb Z,0\le r<|b|\\a\mathbin\%b=\begin{cases}\begin{align}r\quad b>0\\-r\quad b<0\end{align}\end{cases}$$
This represents the %
operator in Python, which calculates the remainder of the division of two numbers. a % b
is the same as abs(a) % b
, and the result has the same sign as the divisor, b
. For the \$a\mathbin\%b\$ operator, this equality holds:
$$(a\pm b)\mathbin\%b=a\mathbin\%b$$
Proof:
$$a=bq+r\leftrightarrow a\pm b=bq+r\pm b=(bq\pm b)+r=b(q\pm1)+r$$
Moreover, for \$b>0\$, we have:
$$a\mathbin\%b=a\leftrightarrow r=a\leftrightarrow0\le a<b$$
Proof for \$r=a\leftarrow0\le a<b\$:
$$0\le a<b\leftrightarrow0\le bq+r<b\leftrightarrow bq=0\leftrightarrow a=r$$
Similarly, for \$b<0\$, we have \$b<a\le0\$.
Therefore, \$a\mathbin\%b=a\leftrightarrow\begin{cases}\begin{align}0\le a<b\quad b>0\\b<a\le0\quad b<0\end{align}\end{cases}\$, or, equivalently, \$(0\le a<b)\lor(b<a\le0)\$.
Functions are allowed to print as programs do. A recursive function that prints can be shorter than both a pure function and a pure program.
Compare these Python 2 submissions to make a list of iteratively floor-halving a number while it's positive, like 10 -> [10, 5, 2, 1]
.
# 30 bytes: Program
n=input()
while n:print n;n/=2
# 29 bytes: Function
f=lambda n:n*[0]and[n]+f(n/2)
# 27 bytes: Function that prints
def g(n):1/n;print n;g(n/2)
The function that prints uses 1/n
to terminate with error on hitting n=0
after having printing the desired numbers. This saves characters over the program's while
and the pure function's base case, giving it the edge in byte count. Often, the termination can be shorter as part of the expression to print or the recursive call. It might even happen on its own for free, like terminating on an empty string when the first character is read.
The key property of our function here is that we're repeatedly applying an operation and listing the results at each step, in order. Additional variables can still be used this way by having them as optional inputs to the function that are passed in the recursive call. Moreover, because we're def
'ing a function rather than writing a lambda
, we can put statements such as variable assignments in its body.
Note: The below makes sense only in the program is scored as characters, not as bytes.
I haven't seen this, though somebody may have posted it somewhere.
I needed to have some long literal ASCII strings in the code so somehow shortening them (as characters, not bytes) would be beneficial. After some experiments I came up with what I call the "Chinese reencoding". I call it that way because ASCII characters mostly seem to be squashed in unicode code points that represent Chinese characters. You take an ASCII string S, encode it in bytes as ASCII, and then decode it in UTF16-BE, like that:
E=S.encode().decode('utf16-be')
The resulting string is half the length. It has to be big endian, as the reverse reencoding may not work - and on most systems the shorter 'utf16' is little endian. You also may need to add a character like space if the original string has odd length, but many times this is OK. Also, for non ASCII characters this does not save length, because they result in too big unicode code points that are represented in the liong form ("\uXXXX")
In you code, use the following:
[E].encode('utf16-be').decode()
in order to get the original longer string, where [E] is the literal shortened string. This costs 29 additional characters, so the original string has to be longer than 58, obviously.
One example - below is my 12 days of Christmas (it can be shortened additionally, but let's use that as an example):
for i in range(12):print('On the %s day of Christmas\nMy true love sent to me\n%s'%('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth Eleventh Twelfth'.split()[i],'\n'.join('Twelve Drummers Drumming,+Eleven Pipers Piping,+Ten Lords-a-Leaping,+Nine Ladies Dancing,+Eight Maids-a-Milking,+Seven Swans-a-Swimming,+Six Geese-a-Laying,+Five Gold Rings,+Four Calling Birds,+Three French Hens,+Two Turtle Doves, and+A Partridge in a Pear Tree.\n'.split('+')[11-i:])))
It's 477 characters long. Let's apply the "Chinese" trick to the two longer string:
r=lambda s:s.encode('utf-16be').decode();for i in range(12):print('On the %s day of Christmas\nMy true love sent to me\n%s'%(r('䙩牳琠卥捯湤⁔桩牤⁆潵牴栠䙩晴栠卩硴栠卥癥湴栠䕩杨瑨⁎楮瑨⁔敮瑨⁅汥癥湴栠呷敬晴栠').split()[i],'\n'.join(r('呷敬癥⁄牵浭敲猠䑲畭浩湧Ⱛ䕬敶敮⁐楰敲猠偩灩湧Ⱛ呥渠䱯牤猭愭䱥慰楮本⭎楮攠䱡摩敳⁄慮捩湧Ⱛ䕩杨琠䵡楤猭愭䵩汫楮本⭓敶敮⁓睡湳ⵡⵓ睩浭楮本⭓楸⁇敥獥ⵡⵌ慹楮本⭆楶攠䝯汤⁒楮杳Ⱛ䙯畲⁃慬汩湧⁂楲摳Ⱛ周牥攠䙲敮捨⁈敮猬⭔睯⁔畲瑬攠䑯癥猬\u2061湤⭁⁐慲瑲楤来\u2069渠愠健慲⁔牥攮ਠ').split('+')[11-i:])))
That's 362, including the lambda (it happens to be worth it, as it is used twice).
Now, all code is mostly ASCII characters, so you may have already guessed that you can use that with exec. There is higher overhead - 43 chars for "exec(''.encode('utf-16be').decode())" (in addition to the whole compressed program) and you may need to double escape some escaped characters in your literal strings (like '\n' in mine has to become '\n'). As a bonus you can always easily add that one space. The compressed porogram looks like:
exec("景爠椠楮\u2072慮来⠱㈩㩰物湴⠧佮⁴桥‥猠摡礠潦⁃桲楳瑭慳屮䵹⁴牵攠汯癥\u2073敮琠瑯\u206d敜渥猧┨❆楲獴⁓散潮搠周楲搠䙯畲瑨⁆楦瑨⁓楸瑨⁓敶敮瑨⁅楧桴栠乩湴栠呥湴栠䕬敶敮瑨⁔睥汦瑨✮獰汩琨⥛楝Ⱗ屮✮橯楮⠧呷敬癥⁄牵浭敲猠䑲畭浩湧Ⱛ䕬敶敮⁐楰敲猠偩灩湧Ⱛ呥渠䱯牤猭愭䱥慰楮本⭎楮攠䱡摩敳⁄慮捩湧Ⱛ䕩杨琠䵡楤猭愭䵩汫楮本⭓敶敮⁓睡湳ⵡⵓ睩浭楮本⭓楸⁇敥獥ⵡⵌ慹楮本⭆楶攠䝯汤⁒楮杳Ⱛ䙯畲⁃慬汩湧⁂楲摳Ⱛ周牥攠䙲敮捨⁈敮猬⭔睯⁔畲瑬攠䑯癥猬\u2061湤⭁⁐慲瑲楤来\u2069渠愠健慲⁔牥攮屮✮獰汩琨✫✩嬱ㄭ椺崩⤩".encode('utf-16be').decode())
and it's 299 characters long. You can see some high code points can always appear. I have not found a way to eliminate them, as the added handling code is not worth the benefit.
This is a cheap trick, in fact, but it can always be applied on top of your solution when the program is longish and there are no or few non-ASCII characters. Often you can devise a custom encoding that can stuff more than two ASCII chars in an unicode one, but it is specific for the task.
exec(bytes('compressed code','u16')[2:])
is a shorter way to achieve this.
\$\endgroup\$
– dingledooper
Jan 12 at 0:42
To set lots of variables to the same thing use:
# x is a list of the names of the variables you want and y is
# the value you want all of them to have
exec(("%s="*len(x))%tuple(x)+str(y))
for c in x:locals()[c]=y
or for c in x:globals()[c]=y
might also work.
\$\endgroup\$
– ovs
Jun 12 '20 at 13:59
Usually you do it this way (6 bytes):
n%2==0
But you can reduce it to 5 bytes:
n%2<1
And even 4 bytes:
~n&1
Bonus tip: when you use if
you can ignore spacebetween if
and ~n&1
this way:
if~n&1:
Suppose we have to print a list as a string with spaces, like square of numbers upto 10
. Then,
print(' '.join(str(i**2)for i in range(11))) # 44 chars
print(*(i**2for i in range(11))) # 32 chars
If you have multidimensional array of numbers and for instance need to count all numbers greater than n.
First flatten the array, then apply filter function to match condition:
l=[[1,[8,4,7,1],3],[5,[7],3,9],[7,3,9,[[[8]]]]]
n=5
flatten=lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l]
len(filter(lambda x:x>n,flatten(l)))
To find the all the indexes of a certain element in a list l
, use
filter(lambda x:l[x]==element,range(len(l)))
To find the next index after a certain index:
l[:index].index(element)
To find the n
th index:
list(filter(lambda x:l[x]==element,range(len(l))))[n]
Say for example you have some output that will be 1, 0, or -1
and you need a different output for each case. You could do something like this:
print('0'if x==0else('1'if x>0else'-1'))
However, the better way is to use x
as an index to a list like so:
print(['0','1','-1'][x])
which is 16
bytes shorter.
print([0,1,-1][x])
\$\endgroup\$
– Felipe Nardi Batista
Jul 12 '17 at 14:00
You can assign to a list inside of a for loop.
For example:
L=[1, 2, 3, 4, 6]
queue = [None]*len(L)
for e, queue[e] in enumerate(L):
print("adding", queue[e], "to processing queue")
This can also be helpful if you need to switch the object you're assigning to.
class Foo:
def __init__(self):
self.x = None
a = Foo()
b = Foo()
for q, (lambda x: a if x%2==0 else b)(q).x in enumerate(range(10)):
print(a.x, b.x)
Here's a little Python 2 snippet that takes a module and a string, and renames every function in that module whose name is longer than 2 characters to a single character with the provided string prefixed. If you're writing a VERY LONG python program that uses many library or builtin functions (and if you manage to golf this snippet better than I have), it has the potential to save quite a few characters. On short programs or programs that use few functions, it will be useless. Since dir() sorts the names in a module, this will always provide the same names to the same functions, and you can use globals() to inspect which names it has given to which functions.
import string
def _(x,y):
for c,f in zip(string.letters,[x.__dict__[q]for q in dir(x)if q in x.__dict__ and(len(q)>2)*type(x.__dict__[q]).__name__.find('eth')>0]):globals()[y+c]=f
You can use it to rename all the string and builtin functions like so:
_(str,'s')
_(__builtins__,'')
And then see what you actually ended up naming them like so:
for k in sorted(globals().keys(),key=lambda x:`len(x)`+x):print k,globals()[k]
If you only want to rename the builtin functions, it's best not to define the function and just use the body directly:
import string
b=__builtins__
for c,f in zip(string.letters,[b.__dict__[q]for q in dir(b)if(len(q)>2)*type(x.__dict__[q]).__name__.find('eth')>0]):globals()[c]=f
Pathlib from shorter files manipulations:
# get current dir, go up one dir, go down one dir, list all "py" files
# and get the whole file bytes
import os.path as p
import glob
d = p.join(p.dirname(p.abspath(__file__))), 'foo', '*.py')
for x in glob.glob(d):
with open(x, 'rb') as f:
do_stuff(f)
Becomes:
import pathlib as p
d = p.Path(__file__).absolute().parent / 'foo'
for f in d.glob('*.py'):
do_stuff(f.read_bytes())
Empty : `a==[]` but just checking if it's non empty and swapping the if and the else can be shorter
Non-Empty : `a` (assuming it is in a situation where it will be interpreted as a boolean)
len(a)>i : `a>a[:i]` if the list is non-empty
[]
is falsy, if want to check if a list is not empty, you can simply do if a:
.
\$\endgroup\$
– Backerupper
Dec 22 '17 at 21:08
1==len(a)
also works for that.
\$\endgroup\$
– Ørjan Johansen
Dec 22 '17 at 21:44
a<a[:2]
is shorter.
\$\endgroup\$
– xnor
Dec 23 '17 at 5:25
You can write
[f(x)for x in l]+[g(x)for x in l]
As
sum([[f(x),g(x)]for x in l],[])
It gets even better with more comprehensions or if you have to take out more values
If you need to expand a list you can even turn l+[f(x)for x in l]+[g(x)for x in l]
into sum([[f(x),g(x)]for x in l],l)
[i for i in range(10)]+[-i for i in range(10)]
returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
, while sum([[i,-i]for i in range(10)],[])
returns [0, 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9]
.
\$\endgroup\$
– Erik the Outgolfer
Jun 29 '18 at 22:29
Large hard coded numbers can be represented in larger bases, but there is a trade off. Higher bases only become worthwhile after a certain cutoff.
The only three bases you're likely to need to worry about are 10
, 16
, and 36
. These are the cutoffs:
1000000000000 (13 bytes) -> 0xe8d4a51000 (12 bytes)
0x10000000000000000000000000000000000000 (40 bytes) -> int("9gmd8o3gbbaz3m2ydgtgwn9qo6xog",36) (39 bytes)
In Python,
True == 1 # true
False == 0 # true
So,
(a<b)*2-1
returns 1 if b is larger than a. If not, returns -1.
More golfing,
-(a>b)|1
returns exactly same value as mentioned above.
Useful when modify iterator index by comparable values.
from lib import func as F
from lib import*;F=func
import lib;F=lib.func
#2 is better than #1 except in rare cases where something in lib clobbers another name that's important to you.
#3 uses lib
twice, winning with short library names.
from lib import*;F=func
is shorter than import lib;F=lib.func
for six letter names; it is always shorter than from lib import func as F
.
\$\endgroup\$
– Dennis
Dec 13 '18 at 4:03
import*
trick is covered in a couple of tips farther up the list from here
\$\endgroup\$
– Sparr
Dec 13 '18 at 21:16
import lib;F=lib.func
to from lib import*;F=func
in this answer.
\$\endgroup\$
– Dennis
Dec 13 '18 at 21:44
I discovered a clever trick used here.
Instead of using the for loop to repeat multiple times, repeat exec multiple times.
p='+'
i=1
exec"print[p*i,i/9*p+'[>'+p*9+'<-]>'+i%9*p][i>20];i+=1;"*255
Compare this with
print"\n".join(">"+"+"*(i/16)+"[<"+"+"*16+">-]<"+"+"*(i%16)if i>31 else"+"*i for i in range(256))
1 or 0 can act as boolean operators in Python:
func = lambda x:1 if x//2==x/2 else 0
while 1:
if func(n):
print('Hello')
else:
exit()
Which is 10 characters shorter than:
func = lambda x:True if x//2==x/2 else False
while True:
if func(n):
print('Hello')
else:
exit()
func
? You could just have func= lambda x:x//2-x/2
and reverse the consequences of the if
(the subtraction will give 0 if they are the same, +/-1 if they are different). It might be better to point out that any python type that has a definition for __bool__
or __non_zero__
could be used instead of a boolean.
\$\endgroup\$
– FryAmTheEggman
Oct 23 '14 at 20:24
return True if condition else False
can always be simplified to return condition
, or if the condition isn't a boolean and you need a boolean, use return bool(condition)
or return condition!=0
if it's a number.
\$\endgroup\$
– Cyoce
Feb 17 '16 at 8:02
while True
is unnecessary. It could be shortened to while func(n):print('Hello')\nexit()
where \n
is the new line character
\$\endgroup\$
– Cyoce
Jul 7 '16 at 2:38
Not read all the answers but you can instead of
if x==3:
print "yes"
else:
print "no"
use
print "yes" if x==3 else "no"
This will work for CPython (probably both 2 and 3), and lets you maybe shave a few bytes if you need to use a lot of different functions and classes with long names from the same module, but you aren't using any of them often enough to rename individually. You'll have to do some research first to figure out which magic numbers give you which functions. Example (rot13):
d=sorted
e=".__dict__.values()"
b=d(eval("__builtins__"+e))
s=d(eval("str"+e))
t=b[12]('string').maketrans
r=''.join(map(chr,range(65,91)))
w=r[13:]+r[:13]
l=t(r+s[4](r),w+s[4](w))
print s[28](b[53](),l)
Translated back to plain python, this is the same as:
t=__import__('string').maketrans
r=''.join(map(chr,range(65,91)))
w=r[13:]+r[:13]
l=t(r+r.lower(),w+w.lower())
print raw_input().translate(l)
which is obviously much shorter, but it should be clear how this methodology would eventually save bytes on much longer, more complicated programs that use more modules.
Slicing can assign:
Converting BGR pixels array to RGB:
l = len(pixels)
for idx in range(0, l - 2, 3):
pixels[idx + 2], pixels[idx] = pixels[idx], pixels[idx + 2]
Becomes:
l = len(pixels)
pixels[2:l:3], pixels[0:l:3] = pixels[0:l:3], pixels[2:l:3]
It's not just shorter. It's 5 times faster. Except on pypy, where the first version is 2x times faster :)
pixels[2::3]
should work too
\$\endgroup\$
– wastl
Jun 2 '18 at 19:26
Tricks with dicts:
# create a dict from iterable
d = {k: None for k in iterable}
# merge 2 dicts
d.merge(d1)
d.merge(d2)
# access a key, if it doesn't exist set a default value and return it
if 'foo' not in d:
res = d['foo'] = 'bar'
res = 'bar
else:
res = d['foo']
Becomes:
d = {**dict.fromkeys(iterable), **d1, **d2}
res = d.setdefault('foo', 'bar')
Or if you need repeated access:
import collections as c
d = c.ChainMap(dict.fromkeys(iterable), d1, d2, {'foo': 'bar'})
res = d['foo']
dict.get
as a first-class function# all keys in the dict G with a truthy value
[k for k in G if G[k]]
filter(G.get,G)
# all keys in the dict G with a falsy value
[k for k in G if not G[k]]
G.keys()-filter(G.get,G)
{…, **d}
to merge dict
s in Python 3# merge two dicts
a={…}
b={…}
merged: {**a,**b} # the order lets you decide which overrides which
# set a defaut value
G.setdefault(a,1)
G[a]=G.get(a,1)
G={a:1,**G}
::
# This is a very specific tip when you want to get both an element at
# the near beginning of a list and one somewhere near the end.
# For example, let's assume you want to take the elements at indices 6 and 37
# from a list L of length 40 (it's important it's < 6+37*2)
# ok
a=L[5];b=L[36]
# equivalent
a,b=L[5],L[36]
# 36 = 5 + 31
a,b=L[5::31]
set()
is 5 chars just to create an empty set. If you can have an initial element e
, you can save 2 chars with {e}
.
# assuming the function iterates on its argument
f([x**2 for x in range(4)])
f(x**2 for x in range(4))