The REALLY bad thing about Python lists ..

Andrew Dalke dalke at acm.org
Tue May 16 15:50:29 EDT 2000


P.S.:

  If there was a way to modify the preallocation size, then it's easy
to modify the growth behaviour.  Support Python lists support a
"reserve(n)" method but with the "grow by constant size" it currently
has.  Then a scalable list could be written as:

class ScalableList(UserList):
  def append(self, item):
    if len(self.data) == self.data.capacity():
      self.data.reserve(len(self.data) * 2)
    UserList.append(self, item)

Much easier to do it this way than emulating the preallocation I
sketched earlier.  Those interested in minimizing the memory waste
for preallocation can change len(self.data) to len(self.data) + 1,
at the possible cost of O(n**2) operations.

                    Andrew
                    dalke at acm.org







More information about the Python-list mailing list