Python List insert() Method


The Python insert() method adds one item to a list at a specified index. Existing items at and after that position move one place to the right.

Unlike append(), which always adds an item at the end, insert() lets us choose where the new item should be placed.

Syntax of insert() 🔝

my_list.insert(index, item)

The insert() method accepts two arguments:

  • index - the position where the new item will be inserted.
  • item - the value or object to add to the list.

Python lists use zero-based indexing, so index 0 refers to the first position.

Basic insert() Example 🔝

In this example, 'Ravi' is inserted at index 1.

my_list=['Alex', 'Ronald', 'John']

my_list.insert(1, 'Ravi')

print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']

The item previously at index 1, 'Ronald', is not removed. It moves to index 2.

Insert an Item at the Beginning 🔝

Use index 0 to insert a new item at the beginning of a list.

my_list=['Ronald', 'John']

my_list.insert(0, 'Alex')

print(my_list)
Output
['Alex', 'Ronald', 'John']

The existing items move one position to the right to make space for the new first item.

Insert an Item at the End 🔝

The len() function returns the number of items in a list. We can use that value as the index to insert a new item at the end.

my_list=['Alex', 'Ronald', 'John']

my_list.insert(len(my_list), 'Ravi')

print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']

This produces the same final position as append(). When the item simply needs to be added at the end, append() is clearer.

Using a Negative Index with insert() 🔝

The insert() method also accepts negative indexes. A negative index counts positions from the end of the list.

my_list=['Alex', 'Ronald', 'John']

my_list.insert(-1, 'Ravi')

print(my_list)
Output
['Alex', 'Ronald', 'Ravi', 'John']

Index -1 refers to the position before the current last item, so 'Ravi' is inserted before 'John'.

Index Greater Than the List Length 🔝

If the supplied index is greater than the current list length, Python inserts the item at the end of the list.

my_list=['Alex', 'Ronald', 'John']

my_list.insert(100, 'Ravi')

print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']

Python does not create empty positions between the existing items and the new item. The value is simply placed at the end.

Return Value of insert() 🔝

The insert() method modifies the existing list in place and returns None.

my_list=['Alex', 'John']

result=my_list.insert(1, 'Ronald')

print(result)
print(my_list)
Output
None
['Alex', 'Ronald', 'John']

Because insert() returns None, we normally call the method directly without assigning its return value.

Do Not Assign insert() Back to the List

my_list=['Alex', 'John']

my_list=my_list.insert(1, 'Ronald')

print(my_list)
Output
None

The original list was modified by insert(), but assigning its return value back to my_list replaces the variable with None.

insert() Does Not Replace an Existing Item 🔝

Using insert() at an existing index adds a new item before the current item. It does not replace the value already stored at that index.

my_list=['Alex', 'Ronald', 'John']

my_list.insert(1, 'Ravi')

print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']

To replace the value at index 1 instead, assign a new value directly to that index.

my_list=['Alex', 'Ronald', 'John']

my_list[1]='Ravi'

print(my_list)
Output
['Alex', 'Ravi', 'John']

Difference Between insert() and append() 🔝

Both methods add an item to an existing list, but the position is handled differently.

  • insert(index, item) adds an item at a specified position.
  • append(item) always adds an item at the end.

Using insert()

my_list=['Alex', 'Ronald', 'John']

my_list.insert(1, 'Ravi')

print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']

Using append()

my_list=['Alex', 'Ronald', 'John']

my_list.append('Ravi')

print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']

Use insert() when the new item's position matters. Use append() when the item should simply be added at the end.

Using insert() with Numbers 🔝

The item being inserted can be a number or another Python object.

my_list=[1, 5, 2, 3]

my_list.insert(2, 22)

print(my_list)
Output
[1, 5, 22, 2, 3]

The value 22 is inserted at index 2. The value previously at that position moves to the right.

Checking List Length After insert() 🔝

Adding one item with insert() increases the number of elements in the list by one.

my_list=['Alex', 'Ronald', 'John']

print("Length before insert:", len(my_list))

my_list.insert(1, 'Ravi')

print("Length after insert:", len(my_list))
print(my_list)
Output
Length before insert: 3
Length after insert: 4
['Alex', 'Ravi', 'Ronald', 'John']

Summary of insert() 🔝

  • insert() adds one item at a specified index.
  • Its syntax is list.insert(index, item).
  • The new item is inserted before the item currently at the specified index.
  • Existing items are shifted to the right instead of being replaced.
  • Index 0 inserts an item at the beginning.
  • len(my_list) can be used to insert an item at the end.
  • Negative indexes can also be used.
  • An index greater than the list length places the item at the end.
  • The method modifies the existing list and returns None.
  • Use append() when the new item only needs to be added at the end.
All List Methods append() extend() Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer