Terry Hancock hancock at anansispaceworks.com
Sun Jun 19 23:25:13 EDT 2005

```On Sunday 19 June 2005 05:34 pm, Xah Lee wrote:
> in coding Python yesterday, i was quite stung by the fact that lists
> appened to another list goes by as some so-called "reference". e.g.
>
> t=range(5)
> n=range(3)
> n='m'
> t.append(n)
> n='h'
> t.append(n)
> print t

Day one in learning Python, yes --- "names bind to objects" NOT
"variables are filled with values".   This is one case where prior
experience with C warps your brain.

> in the following code, after some 1 hour, finally i found the solution
> of h[:]. (and that's cheating thru a google search)
>
> def parti(l,j):
>     '''parti(l,j) returns l partitioned with j elements per group. If j
> is not a factor of length of l, then the reminder elements are dropped.
>     Example: parti([1,2,3,4,5,6],2) returns [[1,2],[3,4],[5,6]]
>     Example: parti([1,2,3,4,5,6,7],3) returns [[1,2,3],[4,5,6]]'''
>     n=len(l)/j
>     r=[] # result list
>     h=range(j) # temp holder for sublist
>     for n1 in range(n):
>         for j1 in range(j):
>             h[j1]=l[n1*j+j1]
>         r.append( h[:] )
>     return r

def parti(L, j):
return [L[k*j:(k+1)*j] for k in range(len(L)/j)]

e.g.:
>>>
>>> parti([1,2,3,4,5,6,7],3)
[[1, 2, 3], [4, 5, 6]]
>>> parti([1,2,3,4,5,6],2)
[[1, 2], [3, 4], [5, 6]]

> PS is there any difference between
> t=t+[li]
> t.append(li)

No, but

t=t+[li]

is quite different from

t.append([li])

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

```