[issue6326] Add a "swap" method to list
Kristján Valur Jónsson
report at bugs.python.org
Tue Jun 23 15:12:49 CEST 2009
New submission from Kristján Valur Jónsson <kristjan at ccpgames.com>:
It is sometimes useful to be able to swap the contents of two lists and
this patch provides a way to do so using the fastest way possible.
> a = [1, 2]
> b = [3]
> id(a), id(b)
(100, 101)
> a.swap(b)
> a
[3]
> b
[1, 2]
> id(a), id(b)
(100, 101)
One application of this is to make help a performance problem when one
wants to upgrade a list instance into a subclass instance.
orglist = rawlist_from_server()
mylist = ListSubclass(orglist)
This involves copying duplicating the list, and then discarding, which
can take a while for long lists. Much faster is using>
mylist = ListSubclass()
mulist.swap(orglist)
We are using this extension within CCP to decoratate database queries
from our database engine, that are returned as python lists. The
performance gained by shaving off this extra list duplication can be
significant for large data sets.
to change a list, into a
----------
components: Interpreter Core
files: listswap.patch
keywords: patch, patch
messages: 89626
nosy: krisvale
severity: normal
status: open
title: Add a "swap" method to list
type: feature request
versions: Python 2.7
Added file: http://bugs.python.org/file14341/listswap.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6326>
_______________________________________
More information about the Python-bugs-list
mailing list