Python List insert()

In this tutorial, we will see about Python List‘s insert method.Python List insert method is used to insert element to the specified index in the list.

Python List insert syntax

here list1 is object of list.
list1.insert(1, ‘hello’): This code will add ‘hello’ string to 1st index of list1.Please note that Python index starts from 0 rather than 1.

Python List insert example

You can simply use insert method to add the element to the list at specified index.
Let’s understand this with the help of simple example.

Output:

[‘Book’, ‘Chair’, ‘Table’, ‘TV’]

As you can see with the output, ‘Chair’ string is added to listOfItems at 1st index.
You can add set, tuple to the list using insert method.Let’s see this with the help of example.

Output:

[‘Book’, ‘Table’, (‘Car’, ‘Cycle’), ‘TV’]

That’s all about Python List insert method.

Was this post helpful?

Leave a Reply

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