On 7/20/07, Bob Gailer <[EMAIL PROTECTED]> wrote:

Take advantage of slicing:
   def create_grid(self):
       table = []
       for i in range(0, len(self.total_num_of_items),
self.max_num_of_items_per_row):
         table.append(tuple(self.total_num_of_items[i : i +
self.max_num_of_items_per_row]))
       return table


simply amazing.  Thank you.

OK - to address your original question:

    def create_grid(self):
        table = []
        while self.total_num_of_items:
            row = []
            count = 0
            while count < self.max_num_of_items_per_row and
self.total_num_of_items:
                row.append(self.total_num_of_items.pop(0))
                count += 1
            table.append(tuple(row))
        return table


At first I  regarded you with quiet awe, but then the nitpick in me saw the
two "while self.total_num_of_item" statements, and I was less pleased.
However, I see this as a doable challenge you have given me, and I will
attempt to optimize your revisions.  Thanks again.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to