Open In App

Code Golfing in Python

Code Golf in Python refers to attempting to solve a problem using the least amount of characters possible. Like in Golf, the low score wins, the fewest amount of characters “wins”. 

Python is a fantastic language for code golfing due to backward compatibility, quirks, it being a high-level language, and all the coercion. So, here we will look at some Code golfing techniques in Python language. 



Using loops

Collapse two numerical loops: Suppose you are iterating over a matrix of m rows and n columns. Instead of two nested for loops, one for the row and one of the columns, it’s usually shorter to use a single loop to iterate over the m*n matrix. 

Original Code : 



m = n = 3
for i in range(m):
    for j in range(n):
        print(i, j)

Golfed Code :  

m = n = 3
for i in range(m*n):
    print(i//n, i%n)

This technique is not limited to only two nested loops, we can even write the same loop for 3 or more nested loops  

m, n, o = 2, 3, 4
for k in range(m*n*o):
    print(k//n//o, k%(n*o), k%o)

Using operators

This works because the bit flip ~x equals -x-1. This uses the same number of characters, but can indirectly cut spaces or parents for operator precedence.

S = {1, 2, 3, 4, 5, 6, 7}
if 5 in S:
    print("Present")
else:
    print("Absent")

Golfed Code: 

S = {1, 2, 3, 4, 5, 6, 7}
if {5}&S:
    print("Present")
else:
    print("Absent")
if a and b:
    print("geeks")
else:
    print("geeksforgeeks")

Golfed Code: 

if a*b:
    print("geeks")
else:
    print("geeksforgeeks")
if a or b:
    print("geeks")
else:
    print("geeksforgeeks")

Golfed Code: 

if a|b:
    print("geeks")
else:
    print("geeksforgeeks")
A.append(B)

Golfed Code: 

A+=B,

Note: B, here creates a one-element tuple which can be used to extend A just like [B] in A+=[B]. 

A.extend(B)

Golfed Code: 

A+=B
if a>1 and a<10:
    print(a)

Golfed Code: 

if 1<a<10:
    print(a)

Note: We can use this technique for multiple variables also at the same time. 

Original Code: 

if a > 10 and b > 10 and 30 > a and 50 > b:
    print(a)

Golfed Code: 

if 30 > a > 10 < b < 50:
    print(a)

Using Functions

from math import floor
n = 3/2
print(floor(n))

Golfed Code: 

n = 3/2
print(n//1)
from math import ceil
n = 3/2
print(ceil(n))

Golfed Code: 

n = 3/2
print(-(-n//1))
def c(a):
  if a < 3: return a-5
  else: return a+5

Golfed Code: 

c=lambda a:a<3and a-5or a+5
string = 'abcdefghijklmnopqrstuvwxyz'
or 
string = [chr(i+97)for i in range(26)]

Golfed Code: 

string = map(chr,range(97,123))

Using indexing

if a<b:return a
else:return b

Golfed Code: 

return (b, a)[a<b]
string = 'geeksforgeeks'
for i in range(len(string)-1,-1,-1):
    print(string[i])

Golfed Code: 

string = 'geeksforgeeks'
for i in string[::-1]:
    print(i)
string = 'geeksforgeeks'
for i in range(len(string)-1,-1,-1):
    print(string[i])

Golfed Code: 

for i in range(len(string)):
    print(string[~i])
A = [1,2,3,4,5,6,7]
for i in A:
    print(i,end = ' ')

Golfed Code: 

A = [1,2,3,4,5,6,7]
print(*A) 

Using assignment

# multiple lines
a = 0
b = 0
c = 0

# single line
a,b,c = 0,0,0

Golfed Code: 

a = b = c = 0
# multiple lines
a = 'p'
b = 'q'
c = 'r'

# single line
a,b,c = 'p','q','r'

Golfed Code: 

a,b,c = 'pqr'

Using conversion

a = (2,3,5,7,11)
x = list(a)

a = 'geeksforgeek'
x = list(a)

Golfed Code: 

a = (2,3,5,7,11)
*x, = a

a = 'geeksforgeeks'
*x, = a
a = (2,3,5,7,11)
x = set(a)

a = [2,3,5,7,11]
x = set(a)

a = 'geeksforgeeks'
x = set(a)

Golfed Code: 

a = (2,3,5,7,11)
x = {*a}

a = [2,3,5,7,11]
x = {*a}

a = 'geeksforgeeks'
x = {*a}
a = (2,3,5,7,11)
x = tuple(a)

a = [2,3,5,7,11]
x = tuple(a)

a = 'geeksforgeeks'
x = tuple(a)

Golfed Code: 

a = (2,3,5,7,11)
x = (*a,)

a = [2,3,5,7,11]
x = (*a,)

a = 'geeksforgeeks'
x = (*a,)

During joining of different iterables 

T = [2,3,4,5,6,7,8,9]
new_T = [1]+T+[10]

Golfed Code: 

T = [2,3,4,5,6,7,8,9]
new_T = [1,*T,10]
T = (2,3,4,5,6,7,8,9)
new_T = (1,)+T+(10,)

Golfed Code: 

T = (2,3,4,5,6,7,8,9)
new_T = (1,*T,10)

Article Tags :