Question

In Python define a function remove_indices which is given a list of items and a list...

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

Homework Answers

Answer #1

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:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a function which returns the number of odd integers in a list (python)
Create a function which returns the number of odd integers in a list (python)
Implement in python a function avg_val(lst), which returns the average value of the elements in list....
Implement in python a function avg_val(lst), which returns the average value of the elements in list. For example, given a list lst: [19, 2, 20, 1, 0, 18], the function should return 10. The name of the method should be avg_val and the method should take one parameter which is the list of values to test. Here is an example call to the function print(avg_val([19, 2, 20, 1, 0, 18]))
Write a Python function which receives 3 lists as its input parameters and combines the lists...
Write a Python function which receives 3 lists as its input parameters and combines the lists and remove repeated numbers from the combined list and return the combined list. For instance, if the input is [1,2,3,4,2,3] and [3,4,6,7] and [-1,0,23,4] the result is [1,2,3,4,6,7,-1,0,23] - Note, the order the lists are combined together does not matter. Use main function.
in python For a list of 100 items, the index of the last item is: (please...
in python For a list of 100 items, the index of the last item is: (please don't use negative indexing here...) Answer:
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Python The function cube should receive a list as a parameter and return a new list...
Python The function cube should receive a list as a parameter and return a new list with each number taken in cubic (raised to three). Write the necessary code for the function, as well as an example which uses the function to calculate and print the cubes for the numbers 5, 8 and 9
Implement function swap that takes a 2D list (a list of list of integers) and modifies...
Implement function swap that takes a 2D list (a list of list of integers) and modifies it in-place by swapping the first element in each row with the last element in that row. The return value is None. You may not use slicing. You maynot use any list method like append, insert, etc. Modification must be in-place, you may not move the list itself or any of its sublists to a new memory location. Examples: reverse([[1,2,3],[4,5,6]])        ->    [[3,2,1],[6,5,4]] reverse([[1,2,3,4],[5,6,7,8]])    ->    [[4,2,3,1],[8,6,7,5]]...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
1.Implement a function which will -       + Accept a number, num, and a list, my_list...
1.Implement a function which will -       + Accept a number, num, and a list, my_list as arguments       + return True if num exists in my_list       + return False otherwise 2. Write Python code to implement the following function - def find_in_list(item, my_list):     """     Look for an item from my_list (of items.)     If the item is found, return the index position of the item;     otherwise, return -1.     """       #add your code here...
Programming language is Python: Define a function drawCircle. This function should expect a Turtle object, the...
Programming language is Python: Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT