Maintaining a list

Thomas Philips tkpmep at hotmail.com
Sat Feb 14 01:33:14 CET 2004


I'm teaching myself programming using Python and want to build a list
inside a function (rather like using a static variable in a Fortran
subroutime - the list must not disappear as soon as the subroutine is
exited). What's the simplest way to do this?

I'd like to write something of the form
def myroutine(x)
    mylist = mylist.append(x)
    print mylist
    .
    .
    return 

I'd like to call myroutine over and over again from a main program,
and want it to display the following behavior

1. The first time myroutine is called
myroutine("Item 1")
["Item 1"]

2. The second time myroutine is called
myroutine("Item 2")
["Item 1", "Item 2"]

3. The third time myroutine is called
myroutine("Item 3")
["Item 1", "Item 2", "Item 3"]

etc. etc.

The list must be initialized to an empty list before the first call,
and must be preserved between calls. I have not got to object oriented
programming as yet, so please keep the solution simple.

Sincerely

Thomas Philips



More information about the Python-list mailing list