In Python
define a function remove_indices which is given a list of items and a list of indices to remove. Remove the given indices in place (no extra memory) in linear time
Complete code in Python3:-
# This function removs Elements from 'Items' list
# At indices stored in 'Indices'.
def remove_indices(Items, Indices):
# Marking those Items as 'None' which will be
deleted.
for i in Indices:
Items[i] = None
# Traversing Items list and removing 'None'
Items
i = 0
while i < len(Items):
if Items[i] is None:
del
Items[i]
else:
i += 1
# List of Items
Items = [1, 2, 3, 4, 5, 6, 7, 89, 78]
# Printing List of items before removal
print("List before removing : ", Items)
# List of Indices that will be removed form the List fo
Items.
Indices = [4, 2, 5, 8]
# Removing Indices from list
remove_indices(Items, Indices)
# Printing List of Items after removal
print("List after removing : ", Items)
Screenshot of output:-
Get Answers For Free
Most questions answered within 1 hours.