Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*PYTHON PROGRAM* ~Create an __sub__ method to overload the \'-\' operator so tha

ID: 3824756 • Letter: #

Question

*PYTHON PROGRAM* ~Create an __sub__ method to overload the '-' operator so that it creates a third UniqueList object that contains only the items that are in the first UniqueList but not second UniqueList. ~

UniqueList Program:

def Uniquelist():
menu_item = 0
list = []
while menu_item != 9:
print ("--------------------")
print ("1. Print the list")
print ("2. Add a name to the list")
print ("3. Remove a name from the list")
print ("9. Quit")
menu_item = input("Pick an item from the menu: ")
if menu_item == 1:
current = 0
if len(list) > 0:
while current < len(list):
print (current, ".", list[current])
current = current + 1
else:
print ("List is empty")
elif menu_item == 2:
name = raw_input("Type in a name to add: ")
if name in list:
print("Name already in list")
else :
list.append(name)
elif menu_item == 3:
del_name = raw_input("What name would you like to remove: ")
if del_name in list:
item_number = list.index(del_name)
del list[item_number]
else:
print (del_name," was not found")
  
print ("Goodbye")

Uniquelist()

Explanation / Answer

Here is the logic for you:

def __sub__(list1, list2):
   list3 = []
   for element in list1:
       if element not in list2:
           list3.append(element)
   return list3