[Tutor] >> BANANAS! << Removing a class instance from a list?

D-Man dsh8290@rit.edu
Tue, 6 Mar 2001 12:17:35 -0500


On Tue, Mar 06, 2001 at 06:50:04AM -0800, Chris McCormick wrote:
| Hey folx,
|   Got a weird one here.  I have a little program to
| grow some bananas.  I have a banana class.  It has
| methods for maturing at specified intervals,
| displaying different bitmaps at different life stages,
| and spawning new plants at maturity.
|   The program works by creating a list of the class
| instances:
| 
| bananacount = input('How many bananas do you wanna
| see? ')
| for x in range(0, bananacount):
|     banananame = "banana" + str(x)
|     new_banana = flora.Banana(banananame, 0, 0)
|     bananas.append(new_banana)

Since you have unique names here I would suggest using a dictionary if
the order of bananas in the list doesn't matter.

Use the name as the key and the banana object as the value.  When you
want to remove a particular banana you can use :

bananas = { }
...
del banans[ name_of_banana_to_remove ]


As Danny said, the criteria for removing the banana will determine how
you should remove it, and also how to store it.

-D