Python code to delete an Element from a Specified Position in a given list.
Python code implementation without user-defined functions & classes
Code:
#Python code to delete an Element from a Specified Position in a given list
a = [23, 34, 45, 5, 56, 78, 8769, 90, 123]
print("List before deletion: ", a)
#list.remove(elem)
#-- searches for the first instance of the given element and removes it (throws ValueError if not present)
#But we need to delete the element from a specific position
#Let's Delete element at position 3
#Position 3 will refer to index 2 because list index begins from 0
print("Deleting element at 3rd Position")
a = a[0:2] + a[3:]
print("List after deleting element at 3rd Position: ")
print(a)
print("Deleting element at 6th Position")
a = a[0:5] + a[6:]
print("List after deleting element at 6th Position: ")
print(a)
Output:
List before deletion: [23, 34, 45, 5, 56, 78, 8769, 90, 123] Deleting element at 3rd Position List after deleting element at 3rd Position: [23, 34, 5, 56, 78, 8769, 90, 123] Deleting element at 6th Position List after deleting element at 6th Position: [23, 34, 5, 56, 78, 90, 123]
Python code implementation using function
Code:
# Python code to delete an Element from a Specified Position in a given list using function
a = [23, 34, 45, 5, 56, 78, 8769, 90, 123]
def del_element_list_func(b, index):
position = index
print("List before deletion: ", b)
print("Deleting element at {} Position".format(position))
b = b[0:position - 1] + b[position:]
print("List after deleting element at {} Position: ".format(position, b))
return b
# Let's Delete element at position 3
# Position 3 will refer to index 2 because list index begins from 0
# Call the Function
a = del_element_list_func(a, 3)
# Let's Delete element at position 6
# Position 6 will refer to index 5 because list index begins from 0
# Call the Function
a = del_element_list_func(a, 6)
Output:
List before deletion: [23, 34, 45, 5, 56, 78, 8769, 90, 123] Deleting element at 3rd Position List after deleting element at 3rd Position: [23, 34, 5, 56, 78, 8769, 90, 123] Deleting element at 6th Position List after deleting element at 6th Position: [23, 34, 5, 56, 78, 90, 123]
Python code implementation using Classes
Code:
# Python code to delete an Element from a Specified Position in a given list using class
a = [23, 34, 45, 5, 56, 78, 8769, 90, 123]
class Del_element_list_class(object):
def del_element_list_func(self, b, index):
self.index = index
self.b = b
print("List before deletion: ", self.b)
print("Deleting element at {} Position".format(self.index))
b = b[0:self.index - 1] + b[self.index:]
print("List after deleting element at {} Position: ".format(self.index, self.b))
return self.b
#Creating the Object
Object_1 = Del_element_list_class()
# Let's Delete element at position 3
# Position 3 will refer to index 2 because list index begins from 0
# Call the Function using created Object
a = Object_1.del_element_list_func(a, 3)
# Let's Delete element at position 6
# Position 6 will refer to index 5 because list index begins from 0
# # Call the Function using created Object
a = Object_1.del_element_list_func(a, 6)
print("The list containing even number is {}".format(Object_1.even_list_function()))
Output:
List before deletion: [23, 34, 45, 5, 56, 78, 8769, 90, 123] Deleting element at 3rd Position List after deleting element at 3rd Position: [23, 34, 5, 56, 78, 8769, 90, 123] Deleting element at 6th Position List after deleting element at 6th Position: [23, 34, 5, 56, 78, 90, 123]
Enjoy Python Code By Pythonbaba 🙂