Declaration of an array of unspecified size

Terry Reedy tjreedy at udel.edu
Sun Aug 31 12:14:43 EDT 2003


"Bertel Lund Hansen" <nospamius at lundhansen.dk> wrote in message
news:m814lvc8h44vbd5g1m2iv05r33qg8ag9ta at news.stofanet.dk...
> Hi all
>
> I am relatively new to Python but have som programming
> experience. I am experimenting wit a POP3-program and it's fairly
> easy.
>
> I want to read the mails into an array of lists so I later can
> choose which one to display. But I need to declare an array of
> unknown size before I can use it in the code.

No you don't ;-)  Part of the beauty of Python (and part of its
slower-than-C-ness) is that lists (and dicts) expand to accomodate the
data put in them (up to RAM limits).  Which means, by the way, no
overruns and the consequences thereof.

>How do I manage

# simple version
emails = [] # create empty list.

# choose loop statement:
while anotheremail():
# or
for i in range(number_of_emails()):

  # then add to list
  emails.append(nextemail())

If you write email fetcher as generator, then "emails =
list(emailgetter(*args))".

Terry J. Reedy






More information about the Python-list mailing list