reshape a list?

Fredrik Lundh fredrik at pythonware.com
Mon Mar 6 21:47:55 CET 2006


"KraftDiner" wrote:

> I have a list that starts out as a two dimensional list
> I convert it to  a 1D list by:
>
> b = sum(a, [])
>
> any idea how I can take be and convert it back to a 2D list?

(you could of course keep a pointer to the original 2D list...)

anyway, to split a 1D list up in pieces, use slice notation.  e.g.

    step = 10

    a = []
    for i in range(0, len(b), step):
        a.append(b[i:i+step])

or, in one line:

    a = [b[i:i+step] for i in range(0, len(b), step)]

for more on list slicing, see the Python tutorial.

</F>






More information about the Python-list mailing list