Efficient way to break up a list into two pieces

marwie marwie at gmx.de
Sat Feb 20 20:55:18 EST 2010


On 21 Feb., 02:30, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> Python lists are arrays of pointers to objects, so copying a slice is
> fast: it doesn't have to copy the objects, just pointers. Deleting from
> the end of the list is also quick, because you don't have to move memory,
> just clear some pointers and change the length field.
>
> Splitting such an array without copying data is, essentially, impossible.
> Python lists aren't linked lists.

Well, to split a C array I would simply set l2 to point to l1[10] and
then
change the length of l1 (which I store somewhere else). No copying of
elements
needed. I would have assumed that python can do something like this
with its
internal arrays of pointers, too.

Anyway, this was more a question about coding style. I use
l1.extend(l2) or
l1 += l2 rather than l1 = l1 + l2 because it's as readable and
possibly faster.
I was simply wondering if something similar exists for splitting
lists.




More information about the Python-list mailing list