A simple list question.

Tim Williams (gmail) tdwdotnet at gmail.com
Thu Dec 22 16:08:51 EST 2005


On 22/12/05, Tim Williams (gmail) <tdwdotnet at gmail.com> wrote:
>
>
>
> On 22 Dec 2005 10:14:10 -0800, KraftDiner <bobrien18 at yahoo.com> wrote:
> >
> > Is there a cleaner way to implement this code?
> >
> >                         if len(self.listOfObjects) == 0:
> >                                 self.listOfObjects.append(
> > self.currentObject)
> >                         elif:
> >                                 self.listOfObjects[self.currentSlice] =
> > self.currentObject
> >
> > listOfObjects is a list of lists....
> > If the listOfObjects is [] then I have to use append to add the first
> > list
> > otherwise I can access each list using the index into the list.
> >
>
> I'm assuming from your code that self.listOfObjects always exists (even if
> it is empty) and that self.currentObject is also  list.
>
>    if  (not self.listOfObjects) or (self.listOfObjects[self.currentSlice]
> = self.currentObject):
>                 self.listOfObjects =  self.currentObject
>
> If self.currentObject is not a list or is only sometimes a list then
> change the 2nd line to
>
>                self.listOfObjects =  list( self.currentObject )
>
> HTH :)
> --
>
> Tim Williams


OOPS !!   completely misread your code, should that elif be an else?

Try this:

                        if not self.listOfObjects:
                                self.listOfObjects.append(self.currentObject
)
                               #or:  self.listOfObjects = list(
self.currentObject)
                        elif  self.listOfObjects[self.currentSlice] =
self.currentObject :
                                do something
                        else:





--

Tim Williams
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051222/5bd08706/attachment.html>


More information about the Python-list mailing list