[Tutor] Acting on objects in a list - how to make the change permanent?

Kent Johnson kent_johnson at skillsoft.com
Wed Sep 29 23:54:37 CEST 2004


Adam,

list.remove(item) will remove item from list without needing the index of 
item in list.

Kent

At 09:08 PM 9/29/2004 +0100, Adam Cripps wrote:
>On Tue, 28 Sep 2004 05:51:17 -0400, Kent Johnson
><kent_johnson at skillsoft.com> wrote:
> > Adam,
> >
> > You don't need to recreate the hierarchy of lists in your list of things to
> > delete. When you create the list of things to delete, you must save a
> > reference to the _containing_ list. Look at what is in the tuples in my
> > example below - it is the item to delete, and the list to delete it from.
> > Take another look at my original answer to your question as well, it shows
> > the same thing another way.
> > http://mail.python.org/pipermail/tutor/2004-September/032027.html
> >
> > Kent
><snip>
>
>Kent,
>
>Thanks for this - I've gone underground since your last email and have
>managed to partly get my head around this to produce some code which
>works in deleting an object. However, it doesn't currently delete the
>correct object in the list. I'm finding it pass the instance location
>of the object. Passing the list itself now works, but currently the
>code always deletes the same item position which isn't effective.
>
>Currently the code [1] drills right down to the article level, with no
>other option, so the level code is really not important. The
>returned_list holds the list to delete from in [0] and the item to
>delete in [1]. However, how do I pass the location of the actual item,
>rather than the object itself? If I pass the object itself, it
>correctly throws an error up as only allowing to del with an integer.
>Is there a built in function for returning the position of an item?
>
>Once again, thanks for all the help.
>
>Adam
>
>[1]
>def return_a_list(self, level):
>                 if level == 0:
>                         level = raw_input("What would you like to search 
> for? a - article /
>i - issue / t - title ")
>                 else:
>                         if level == "t" or level == "i" or level == "a": 
> # Only doing a at the moment
>                                 print " I got this far"
>                                 chosen_title_object = self.choosetitle()
>                                 chosen_issue_object = 
> self.chooseissue(chosen_title_object)
>                                 chosen_article_object = 
> self.choosearticle(chosen_issue_object)
>                                 returned_list = 
> [chosen_issue_object.articlesheld, chosen_article_object]
>                                 print returned_list
>                                 return [returned_list]
>
>
>         def choose_what_to_edit(self):
>                 # New attempt to try this one now
>                 level = 0
>                 print " I got to choose_what_to_edit"
>
>                 returned_list = self.return_a_list("a")
>                 print returned_list
>                 raw_input("OK?")
>                 confirm = raw_input("Do you wish to delete this article? 
> (y/n)")
>                 if confirm == "y":
>                         print "will be deleted"
>                         for list, item in returned_list:
>                                 del list[1] # OK this is deleting, but 
> always the first item
>                 else:
>                         print " will not be deleted "
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list