Google News
logo
Python Program to Add an item at a specified index
To add an item at a specific index in a Python list, you can use the `insert()` method of the list object. The `insert()` method takes two arguments : the first argument is the index where you want to add the item, and the second argument is the item itself.

Here is an example Python program that demonstrates how to add an item at a specified index in a list :
Program :
my_list = ["apple", "banana", "cherry"]

# Add "orange" at index 1
my_list.insert(1, "orange")

print(my_list)
Output :
['apple', 'orange', 'banana', 'cherry']
In the above example, we first create a list called `my_list` with three items: "apple", "banana", and "cherry". Then, we use the `insert()` method to add the item "orange" at index 1, which pushes "banana" and "cherry" one index to the right. Finally, we print the updated list using the `print()` function.