Python List insert() Method Tutorial

Python List Insert() Method

This is a detailed tutorial of the Python List insert() method. Learn to insert an item into the list at a particular index using the insert() method.

Python List insert()

The list.insert() method is used to insert an item at a particular index into a given Python List. The new item to be inserted and the index at which it is to be inserted has to be provided as the second and first arguments to this method respectively.

Syntax

The following syntax is to insert the item at the index in the list.

insert() Parameters

As shown in the syntax, the method insert() takes two arguments. These are described below.

  • index. This is the numerical index that represents the position where the item is to be inserted. The first item of any list is at index 0, the second item at index 1 and so on it continues.
  • item. This is the actual item that is to be inserted. It could be any Python Object or may belong to any Data Type.

insert() Return Value

As the list.insert() method simply inserts a new item into the list, so it does not return anything i.e. it returns None.

Examples

The usage of the Python List insert() method is quite easy and is demonstrated below in the form of example code snippets.

Example 1. Inserting a new item (element) to a Python List

Here I’ve defined a list with name students. Then I’m adding a new item "Manik" at index 4 i.e. at the 5th position in the list as the indexing of the list starts from 0 and not from 1.

As you can see in the following screenshot of the output that the list is updated with the new item "Manik" inserted at index 4 while the other items after "Manik" will shift their index forward by 1.

Python List Insert() Method Example

Example 2. Inserting new Collection Objects (Tuple, Set, Dictionary) into a List

As I mentioned earlier in this article that you can insert a new item of any data type into an existing Python List. It does not matter at all that the list items should be of the same data type. Therefore, in the following example, I’m creating a list containing items of multiple collections-based data types.

Here, I’ve first created a list named collections with different collection objects like a Tuple, a List, and a Set as the list items. Then I’m inserting another List, another Tuple, and another Set as individual list items to the collections List using the insert() method at particular indexes.

Python List Insert() Method Example Inserting Elements Of Different Data Types

The above method is to insert new collection objects like another List, Tuple or Set as items of the list. If you want to merge the items of two different collection objects into one collection object, you can use the Python List extend() method or simply the + operator. The following linked articles may help in that case.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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