In: Computer Science
Write a program that removes a target item (a string) from a
list of strings, and
that will print out the list without the item. Do not use built-in
functions other than
input, print, and append. Instead, you may generate a new list that
does not
include the target item and you can use the append() function. Your
program
should read two inputs: the target and the list. For example, if
the input is “apple”
and the list is [‘berry’, ‘apple’, ‘pear’, ‘orange’], the outputs
should be [‘berry’,
‘pear’, ‘orange’] .
I have implemented the code to remove the target in the list per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
lst=[]
#Create a input list
s=input('Enter element to insert(stop to exit) : ')
while s!='stop':
lst.append(s);
s=input('Enter element to insert(stop to exit) : ')
#print the list
print("The list is : ",lst)
#read the target to remove
target=input('Enter target to Remove : ')
#Empty list to store the elements
lst2=[]
#Loop for each element in lst
for s in lst:
#If the string in 's' is not equal to 'target'
#then we append to the list2
if s!=target:
lst2.append(s)
#print the final result
print("The list after removing "+target+" : ")
print(lst2)