Python Code to Insert an Element at a Specified Position in a given list.
Python code implementation without user-defined functions & classes
Code:
#Python Code to Insert an Element at a Specified Position in a given list
a = [23, 34, 45, 56, 78, 90, 123]
print("List before insertion: ",a)
# list.insert(index, elem)
#-- inserts the element at the given index, shifting elements to the right.
#Let's insert element at position 3
#Position 3 will refer to index 2 because list index begins from 0
print("Inserting element at 3rd Position")
a.insert(2, 45)
print("List after inserting element 45 at 3rd Position: ")
print(a)
print("Inserting element at 7th Position")
a.insert(6, 8769)
print("List after inserting element 8769 at 7th Position: ")
print(a)
Output:
List before insertion: [23, 34, 45, 56, 78, 90, 123] Inserting element at 3rd Position List after inserting element 45 at 3rd Position: [23, 34, 45, 45, 56, 78, 90, 123] Inserting element at 7th Position List after inserting element 8769 at 7th Position: [23, 34, 45, 45, 56, 78, 8769, 90, 123]
Python code implementation using function
Code:
#Python Code to Insert an Element at a Specified Position in a given list using function
a = [23, 34, 45, 56, 78, 90, 123]
def insert_element_list(index, element):
b = element
position = index
print("List before insertion: ", a)
a.insert(position, b)
#Let's insert element at position 3
#Position 3 will refer to index 2 because list index begins from 0
print("Inserting element at 3rd Position")
insert_element_list(2, 45)
print("List after inserting element 45 at 3rd Position: ")
print(a)
print("Inserting element at 7th Position")
insert_element_list(6, 8769)
print("List after inserting element 8769 at 7th Position: ")
print(a)
Output:
List before insertion: [23, 34, 45, 56, 78, 90, 123] Inserting element at 3rd Position List after inserting element 45 at 3rd Position: [23, 34, 45, 45, 56, 78, 90, 123] Inserting element at 7th Position List after inserting element 8769 at 7th Position: [23, 34, 45, 45, 56, 78, 8769, 90, 123]
Python code implementation using Classes
Code:
#Python Code to Insert an Element at a Specified Position in a given list using class
a = [23, 34, 45, 56, 78, 90, 123]
class Insert_element_list_class(object):
def insert_element_list_func(self, index, element):
self.index = index
self.element = element
print("List before insertion: ", a)
a.insert(self.index, self.element)
#Create the object:
Object_1 = Insert_element_list_class()
#Let's insert element at position 3
#Position 3 will refer to index 2 because list index begins from 0
print("Inserting element at 3rd Position")
Object_1.insert_element_list_func(2, 45)
print("List after inserting element 45 at 3rd Position: ")
print(a)
print("Inserting element at 7th Position")
Object_1.insert_element_list_func(6, 8769)
print("List after inserting element 8769 at 7th Position: ")
print(a)
Output:
List before insertion: [23, 34, 45, 56, 78, 90, 123] Inserting element at 3rd Position List after inserting element 45 at 3rd Position: [23, 34, 45, 45, 56, 78, 90, 123] Inserting element at 7th Position List after inserting element 8769 at 7th Position: [23, 34, 45, 45, 56, 78, 8769, 90, 123]
Enjoy Python Code By Pythonbaba 🙂