Python

9 Subtle Tricks To Make Your Python Code Much Faster

Small changes, big differences

Yang Zhou
TechToFreedom
Published in
7 min readDec 26, 2023

--

Futuristic digital illustration of a majestic whale soaring through a sky filled with fluffy clouds, illuminated by digital code and light fragments, symbolizing swift, optimized performance in Python programming. The image creatively embodies the concept of making Python code lighter and faster, akin to a whale gliding effortlessly through the clouds.
Image from Wallhaven

“Python is too slow.”

This sentiment echoes frequently in discussions about programming languages, often overshadowing Python’s numerous strengths.

The truth is, Python is fast if you can write it in a Pythonic way.

The devil is in the details. Experienced Python developers are armed with an arsenal of subtle yet powerful tricks to significantly enhance their code’s performance.

These tricks might seem minor at first glance, but they can lead to substantial improvements in efficiency. Let’s delve into 9 of these approaches, transforming the way you write and optimize Python code.

1. Faster String Concatenation: Choose “join()” or “+” Skillfully

String concatenation will become a bottleneck of your Python program if a large number of strings are waiting to be handled.

Basically, there are two ways of string concatenation in Python:

  • Use the join() function to combine a list of strings into one
  • Use the + or += symbol to add every single string into one

--

--