[reportlab-users] list reuse problem

Timothy Smith timothy at open-networks.net
Wed Jul 13 10:01:01 EDT 2005


Timothy Smith wrote:

> Robin Becker wrote:
>
>> Timothy Smith wrote:
>>
>>> i have a situation where i'm using a list of lists for a data in a 
>>> table, then deleteing some elements from it and reusing it, and i'm 
>>> getting this error. i'm double triple checked all the rows in it are 
>>> the same lenght, so i'm a bit stumped as to what this error means.
>>>
>>> File 
>>> "/usr/local/lib/python2.3/site-packages/reportlab/platypus/tables.py", 
>>> line 419, in _hasVariWidthElements
>>>    value = self._cellvalues[row][col]
>>> IndexError: list index out of range
>>>
>> ..... you're probably hitting a mutable re-use problem. The lists are 
>> not copied into the table internally. Deleting a row/column in one 
>> usage will affect all so unless you are careful the error is fairly 
>> easily created.
>>
>> For example
>>
>> >>> A=[[1,2,3],[4,5,6]]
>> >>> B = A
>> >>> del B[0][-1], B[1][-1]
>> >>> A
>> [[1, 2], [4, 5]]
>> >>>
>
>
> so how do i work around it?
> i tried creating a whole new list by
>
> for x in Roster:
>        Roster2.append(x)
>       for i in Roster2:
>     dlist = (4,6,8,10,12,14,16)
>     for x in dlist:
>         del(i[x])
>
> and still i got an error
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
>
>
and here is a neater attempt

    Roster2 = []
    dlist = (4,6,8,10,12,14,16)
    for x,row in enumerate(Roster):
        Roster2.append(row)
        for i in dlist:
            del(Roster2[x][i])

i actually printed out Roster and Roster2 and it's STILL deleteing the 
elements from Roster!



More information about the reportlab-users mailing list