Sling Academy
Home/Python/Using the list.insert() method in Python

Using the list.insert() method in Python

Last updated: June 12, 2023

Overview

In Python, the list.insert() method inserts an element at a given position in a list. Below is its syntax:

list.insert(i, x)

Parameters:

  • i: The index of the element before which to insert, and can be any integer. If it is negative, it counts from the end of the list. If it is larger than the length of the list, it inserts the element at the end of the list
  • x: The element to insert. It can be any object, such as a string, a number, another list, etc. If it is an iterable, such as a list or a tuple, it will insert the whole iterable as a single element (you will understand this better in the examples to come).

The list.insert() method does not return anything. It modifies the original list in place.

The performance of the list.insert() method depends on the size of the list and the position where you insert the element. Its time complexity is O(n). That means it takes O(n) time in the worst case, where n is the length of the list, because it has to shift all the elements after the insertion point to the right.

Now, it’s time for the interesting part: code and examples of using the list.insert() method in practice.

Examples

Inserting a single element at a specific position in a list

The code:

# a list of video games
games = [
    "Minecraft",
    "Fortnite",
    "CS:GO"
]

# Insert a game at index 2
games.insert(2, "League of Legends")
print(games)

Output:

['Minecraft', 'Fortnite', 'League of Legends', 'CS:GO']

Inserting an iterable as a single element in a list

This example demonstrates inserting a tuple into another list in Python:

my_list = [
    "Sling Academy",
    "Python",
    "Programming"
]

my_list.insert(1, (2023, 2024, 2025))
print(my_list)

Output:

['Sling Academy', (2023, 2024, 2025), 'Python', 'Programming']

Inserting an element at the end or the beginning of a list

The code:

my_list = ["one", "two", "three"]

# Insert "zero" to the beginning of the list
my_list.insert(0, "zero")

# Insert "four" to the end of the list
my_list.insert(len(my_list), "four")

print(my_list)

Output:

['zero', 'one', 'two', 'three', 'four']

Next Article: Sorting Lists in Python (4 Examples)

Previous Article: Python: How to Calculate the Average of a Numeric List

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types
  • Python: Typing a function with *args and **kwargs