Python Forum

Full Version: Remove object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can any1 help me with my code? I cant remove object inside of my list. And how to update them.

import pickle
from module2 import cmod2
plannerlist = []

class cmod3:
    def meth31():
        planner = []
        add = cmod2.meth22()
        planner.append(add)
        plannerlist.append(planner)
        pickle.dump(plannerlist, open('pl','wb'))
        print('Your plan:')
        for planner in plannerlist:
            print(*planner)
        return  planner
    #return plannerlist

    #      print(pl)
#show

    def meth32():
        plannerlist = pickle.load(open("pl", "rb"))
        print("Current plan list:")
        for planner in plannerlist:
            print(*planner)
        removeplan = input("Which plan would you like to remove?")
        for planner in plannerlist:
            if planner == removeplan:
                plannerlist.remove(planner)

        pickle.dump(plannerlist, open("pl", "wb"))
        print('Plan removed.')
#remove
What happens when you try to run meth32? And is there some reason you didn't name it remove_plan, or something more descriptive than meth32?
It shows all the object in the list but when I try to remove some of it, nothing happen.
Then I am guessing your equality is failing on line 28. From line 25 planner appears to be an iterable, maybe either a list or a tuple. But the equality is comparing the string from the input on line 26 to the iterable, which is never going to match.

Edit: If one of the items in the iterable is a string that is meant to match the input, you need to compare to that, not to the whole iterable.
I try to put 'str' on removeplan and still nothing happens, what I am missing?
You are comparing a string to a list. '5' is never going to equal [1, 2, 5]. Even if you make a string of the list, you are now comparing '5' to '[1, 2, 5]', which is still not a match, unless you type in the list literal exactly.