How to shorten your Python code

Jakob Samonig
3rd March 2019


Python. It’s probably one of the most useful and versatile programming languages available to date. It can be used for many different things such as: data science (things like machine learning and data analysis), web development and scripting. But as with most pieces of code, Python code can quickly get drawn-out and become inefficient; especially when you are like me and like to copy out things from Stack Overflow, even though you could probably come up with a neater and more efficient solution. So here is a guide on how to improve some of your Python code:

Switching over variables and grouped naming

Instead of doing this:

tmp = y
y = x
x = tmp

do this:

x, y = y, x

This is an easy thing to improve on and once you get the hang of it, you can shorten down your code drastically. You can even use this to declare multiple variables at once:

x, y, z = 1, 2, 3

instead of

x = 1
y = 2
z = 3

Using the range() function for iteration

Instead of doing this:

i = 0
while i < 5:
    print(i)
    i += 1

do this:

for i in range(5):
    print(i)

This is a lot cleaner and easier to read. But be careful as the last number, in the range function, is not inclusive (range(2) would be 0-1).


Ternary operators (only works in Python versions above 2.5)

Instead of:

if t == 0:
    m = 100
else:
    m = 5

do this:

m = 100 if t == 0 else 5

The general syntax for this is the following: [expression]if[condition]else[condition 1].


List comprehension

Instead of:

for item in list:
    if conditional:
        expression

do this:

[expression for item in list if conditional]

The result is effectively the same only that you can do it in only one line which saves lots of space in long programs. List comprehensions only create list however.


Generator expressions

Generator expressions are very weird creatures, but they can be extremely useful: especially for iteration. The general syntax is:

(expression for x in i if condition)

which means:

for x in s:
    if condition:
       yield expression # yield is like return, but for generators

The important differences from a list comprehension is that generator expressions do not construct a list; only real purpose is iteration and once they are consumed, they cannot be reused. So as an example:

a = [1, 2, 3, 4]
b = (2*x for x in a)

However, as these things are quite weird, if I were to print b it would give me: <generator object <genexpr> at 0x0156FD80> . As a result, if you want to do anything with this you have to either convert the variable b to a list or use the inbuilt next() function.


Using nested functions instead of variables

Instead of:

a = exampleFunction(value)
print(a)

do this:

print(exampleFunction(value))

However, if you need to do more than one thing with the result of a function, you need to store that in a variable. This is because repeating functions instead of having a variable is massively inefficient (alternatively, you could use Python's anonymous function Lambda).