How to insert an object in a list at a given position in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array.

In this article, we will show you how to insert an item/object in a list at a given position using python. −

  • Insert an item at a specific position in a list

  • Insert an item at the first position in the list

  • Insert an item at the last/end position in the list

Assume we have taken a list containing some elements. We will insert an object in a list at various index positions using different methods as specified above

Method 1: Insert an item at a specific position in a list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list

  • Enter the item to be inserted into the list and create a variable to store it.

  • Enter the index value at which the item must be inserted.

  • Use the insert() function (inserts the provided value at the specified position) to insert the given item at the specified index into the list by passing the index value and item to be inserted as arguments to it.

list.insert(position, element)
  • Print the list after inserting the given item/object at the specified index

Example

The following program inserts the given item at the entered index position using the insert() function −

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print("List =",lst) # giving the item to be inserted insertItem = "sample" # giving the index value at which the item to be inserted indexValue = 2 # inserting the given list item at the specified index(here 2) lst.insert(indexValue, insertItem) # printing the list after insertion print("The list after inserting the given list item at the specified index:") print("List = ",lst)

Output

On executing, the above program will generate the following output −

List = ['Hello', 'TutorialsPoint', 20, 'python', 'code']
The list after inserting the given list item at the specified index:
List = ['Hello', 'TutorialsPoint', 'sample', 20, 'python', 'code']

We have a sample list with some random data. The element that must be added to the list and the index of the element where it must be entered to give the code The index and element were then passed as arguments to the insert() function, and the list was printed once the element was added.

Method 2: Insert an item at the first position in the list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Enter the item to be inserted into the list and create a variable to store it.

  • Use the insert() function (inserts the provided value at the specified position) to insert the given item at the first position (index=0) into the list by passing the index value as 0 and the item to be inserted as arguments to it.

lst.insert(0, insertItem)
  • Print the list after inserting the given item/object at the first position (index=0).

Example

The following program inserts the given item into the list at the first position using the insert() function −

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print(lst) # giving the item to be inserted insertItem = "sample" # inserting the list item at the first position(index=0) in the list lst.insert(0, insertItem) # printing the list after insertion print("The list after inserting the list item at the first position(index=0) in the list:") print(lst)

Output

On executing, the above program will generate the following output −

['Hello', 'TutorialsPoint', 20, 'python', 'code']
The list after inserting the list item at the first position(index=0) in the list:
['sample', 'Hello', 'TutorialsPoint', 20, 'python', 'code']

We passed the index as 0 since we need to insert the element at the beginning.

Method 3: Insert an item at the last/end position in the list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list

  • Enter the item to be inserted into the list and create a variable to store it.

  • Use the insert() function (inserts the provided value at the specified position) to insert the given item at the end of the list by passing the list length (for getting the length of the list, we will use len() method), and the item to be inserted as arguments to it.

lst.insert(len(lst), insertItem)

    The last index of the list is len(list)-1, so we use len(list) as a parameter to insert an item at the end.

When this index is passed to the insert method, the element is inserted at the end of the list.
  • Print the list after inserting the given item/object at the end of the list.

Example

The following program inserts the given item into the list at the last position using the insert() function −

# input list lst = ["Hello", "TutorialsPoint", 20, "python", "code"] print(lst) # giving the item to be inserted insertItem = "sample" # inserting the list item at the end of the list # len(lst) gives the list length i.e, the last index value lst.insert(len(lst), insertItem) # printing the list after insertion print("The list after inserting the list item at the end:") print(lst)

Output

On executing, the above program will generate the following output −

The list after inserting the list item at the end:
['Hello', 'TutorialsPoint', 20, 'python', 'code', 'sample']

We gave the index as the length of the list since we need to insert the entry at the end.

Conclusion

In this code, we learned how to use the insert() function to insert an element to a list at the beginning, specified index, and at the end.

Updated on: 27-Aug-2023

30K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements