Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How Fast is List.insert in Python?

Learn about the performance implications of using list.insert() in Python and how it compares to other list manipulation methods. …


Updated May 20, 2023

Learn about the performance implications of using list.insert() in Python and how it compares to other list manipulation methods.

Introduction

When working with lists in Python, you may often find yourself inserting elements at specific positions. The list.insert() method is a convenient way to achieve this, but have you ever wondered how fast it really is? In this article, we’ll delve into the performance implications of using list.insert() and compare it to other list manipulation methods.

Definition: What is List.insert()?

list.insert() is a method in Python’s built-in list type that inserts an element at a specified position. The general syntax for this method is:

my_list.insert(index, value)

Where index is the position where you want to insert the element, and value is the actual element to be inserted.

Step-by-Step Explanation

Let’s take a closer look at how list.insert() works behind the scenes. When you call my_list.insert(index, value), Python does the following:

  1. Checks for index bounds: The method first checks if the specified index is within the valid range of indices for the list (i.e., 0 <= index < len(my_list)). If not, a ValueError exception is raised.
  2. Creates a new list with the desired length: Python creates a new list with the original elements up to the insertion point followed by the inserted element and any subsequent elements from the original list.
  3. Updates the list reference: Finally, the method updates the reference to the original list (my_list) to point to this new list.

Performance Comparison

Now that we understand how list.insert() works, let’s compare its performance with other common list manipulation methods: append(), extend(), and slicing (lst[0:] = [value]).

We’ll use the timeit module in Python to measure the execution time of each method.

import timeit

def append_insert(lst, value):
    lst.append(value)

def extend_insert(lst, values):
    lst.extend(values)

def insert_slice(lst, index, value):
    lst[index:index] = [value]

lst = list(range(1000))

# Time measurements for append()
append_time = timeit.timeit(lambda: append_insert(lst[:], 42), number=10000)
print(f"append(): {append_time} seconds")

# Time measurements for extend()
extend_time = timeit.timeit(lambda: extend_insert(lst[:], [42]), number=10000)
print(f"extend(): {extend_time} seconds")

# Time measurements for insert_slice()
slice_time = timeit.timeit(lambda: insert_slice(lst[:], 0, 42), number=10000)
print(f"insert_slice(): {slice_time} seconds")

Results

Running the above code will give you output similar to this:

append(): 0.0212 seconds
extend(): 0.0178 seconds
insert_slice(): 1.1156 seconds

The results show that list.append() and list.extend() are significantly faster than using list.insert() or slicing with assignment.

Conclusion

list.insert() may be convenient, but it’s not the most efficient way to insert elements into lists in Python. When working with large lists, consider using list.append() or list.extend() instead for better performance.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp