lists and append, and loop iteration

Mark Krischer mkrisch at radiata.com
Wed Sep 22 18:46:28 EDT 1999


Fredrik Lundh wrote:
> 
> Mark Krischer <mkrisch at radiata.com> wrote:
> > this might be a newbie a question, but is there a reason why append
> > doesn't create a list if the list doesn't exist?
> 
> catch 22: if you don't have a list, you cannot figure
> out what "append" means.  it's a method of the list
> type, you know...

ah, of course. i'm still thinking in C/C++ where i would have declared
the variable to be of type "list" earlier.

> > and while i'm bending your ear, let me ask a second question.
> >
> > if i do:
> > for element in list:
> > do something with element
> >
> > is there any to find out what my current index is? i've basically been
> > doing something like:
> > list.index(element)
> >
> > this happens to work fine for what i'm doing now, but only because i
> > don't care about doubles in the list, but should that ever be a problem,
> > how will i know which element i'm dealing with.
> >
> > i figure i can always do:
> > for i in range(len(list)):
> > do something with list[i]
> >
> > but it just feels like i should be able to things the other way as well.
> 
> you cannot.  there have been a few proposals to change the
> syntax so you can get at the internal loop counter, but nothing
> has yet made it into the language.

something to keep an eye on then.

thanks.

--mk




More information about the Python-list mailing list