Efficient way to break up a list into two pieces
marwie at gmx.de
Sun Feb 21 02:55:18 CET 2010
On 21 Feb., 02:30, Steven D'Aprano <st... at REMOVE-THIS-
> 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 and
change the length of l1 (which I store somewhere else). No copying of
needed. I would have assumed that python can do something like this
internal arrays of pointers, too.
Anyway, this was more a question about coding style. I use
l1 += l2 rather than l1 = l1 + l2 because it's as readable and
I was simply wondering if something similar exists for splitting
More information about the Python-list