How to insert elements to a Python list at a specific position?

In this post, you will learn how to insert an element at a specific position or location, or Index in a Python list.

Python List insert() method

The python list insert() method is used to insert an element at a specific position in a list.

Syntax of the insert() method

The insert method takes only two parameters: index and item

list.insert(index, item)
  1. Index – The index number where you want to insert an item or element. And the existing elements listed after the specified index will be shifted to the right after the new element.
  2. Item – The item/element that will be inserted in the list.

The list insert() method doesn’t return anything, it just updates the existing list.

>>> a = [45,23,52,14]
>>> a.insert(3, 'Python')
>>> print(a)
[45, 23, 52, 'Python', 14]
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *