Fundamentals 5 min read

Four Time‑Saving Python Tricks to Speed Up Your Code

This article presents four practical Python performance tricks—including list reversal, tuple swapping, in‑function looping, and reducing function calls—that can shave 10–20% off execution time while keeping the code concise and readable.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Four Time‑Saving Python Tricks to Speed Up Your Code

Today I want to share four time‑saving Python tricks that can reduce execution time by roughly 10–20%.

Reverse List

Python provides two common ways to reverse a list: slicing (e.g., mylist[::-1] ) and the reverse() method. The reverse() method mutates the original list, while slicing creates a new one.

Performance testing with python -m timeit shows that reverse() is faster (≈10.7 µs per loop) than slicing (≈15.6 µs per loop) for a list of 200 elements.

Swap Two Values

Python allows swapping two variables in a single line using tuple unpacking:

<code>variable_1, variable_2 = variable_2, variable_1</code>

The same technique works for dictionary entries:

<code>md[key_2], md[key_1] = md[key_1], md[key_2]</code>

This one‑liner avoids temporary variables and reduces execution time.

Loop Inside Function

Placing a for loop inside a function can be faster than calling a function repeatedly inside a loop because the loop overhead is incurred only once.

Example functions:

<code>def only_function(x):
    new_string = x.capitalize()
    output_string = x + " " + new_string
    print(output_string)</code>
<code>def for_in_function(list_of_strings):
    for x in list_of_strings:
        new_string = x.capitalize()
        output_string = x + " " + new_string
        print(output_string)</code>

Timing shows the in‑function loop is slightly faster.

Reduce Function Calls

When checking an object's type, prefer isinstance() (one call) over type() comparisons (two or three calls). Avoid placing repeated operations in loop conditions; compute them once before the loop.

<code># Avoid repeated len() calls
while i < len(a):
    ...
# Compute once
m = len(a)
while i < m:
    ...</code>

Import only the needed objects (e.g., from X import Y ) instead of importing the whole module and accessing attributes, which saves a lookup.

In summary, leveraging Python's built‑in functions and idioms can noticeably speed up programs while keeping the code clean and maintainable. For more details, consult the official Python documentation.

performancePythonSpeedupcode tricks
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.