Python .insert()

Sriparno08's avatar
Published May 5, 2021Updated May 29, 2025
Contribute to Docs

In Python, the .insert() method adds an element at a specific index in a list, shifting subsequent elements to the right.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

list.insert(index, element)

Parameters:

  • index: The position in the list where the element is to be added. If the index is greater than the length of the list, the element is added to the end of the list.
  • element: The element to be added to the list.

Return value:

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

Example 1: Basic Usage of .insert()

This example adds the string "orange" to index 1 in the fruits list:

# Create a list
fruits = ["apple", "banana", "cherry"]
# Add "orange" to index '1' in the list
fruits.insert(1, "orange")
# Print the list
print(fruits)

Here is the output:

['apple', 'orange', 'banana', 'cherry']

Example 2: Adding to the Beginning of the List Using .insert()

This example adds the number 1 to the beginning (index 0) of the numbers list:

# Create a list
numbers = [2, 3, 4, 5]
# Add the number '1' to the beginning of the list
numbers.insert(0, 1)
# Print the list
print(numbers)

Here is the output:

[1, 2, 3, 4, 5]

Codebyte Example: Using Out-of-Bounds Index with .insert()

This codebyte example attempts to add the string "purple" to index 10 in the colors list:

Code
Output

Though the index 10 is out of bounds for the list, Python will gracefully add "purple" to the end of the list.

Frequently Asked Questions

1. What happens if I pass a negative index to .insert()?

Passing a negative index to .insert() will make it start counting from the end of the list.

2. Can I insert multiple elements at once using .insert()?

No, .insert() only allows the addition of a single element at a time.

3. Is .insert() efficient for large lists?

Not particularly. Since .insert() requires shifting elements to accommodate the new item, inserting near the beginning of a large list can be slow. For frequent insertions, consider using collections.deque.

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours