A simple list question.

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


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051222/d0c96120/attachment.html>


More information about the Python-list mailing list