Add Element at Specific Index in Python List

30-Nov-2022

.

Admin

Add Element at Specific Index in Python List

Hi Dev,

This tutorial will provide example of add element at specific index in python list. we will help you to give example of python add element to list in specific position. This post will give you simple example of python list append at index. In this article, we will implement a python list add item at index.

There are many ways to add elements at a specific index to a python list. i will give you two examples using insert() method and list key to add element at specific position in python list. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myList = [1, 2, 3, 4]

# Add new element with specific index

myList.insert(2, "New Elem")

print(myList)

Output:

[1, 2, 'New Elem', 3, 4]

Example 2:

main.py

myList = [1, 2, 3, 4]

# Add new element with specific index

myList[2:2] = ["New Elem"]

print(myList)

Output:

[1, 2, 'New Elem', 3, 4]

#Python