[Python-ideas] clear() method for lists
gerald.britton at gmail.com
Wed Feb 10 15:54:22 CET 2010
On Wed, Feb 10, 2010 at 9:12 AM, Mathias Panzenböck
<grosser.meister.morti at gmx.net> wrote:
> On 02/10/2010 11:39 AM, wxyarv wrote:
>> what about another method clone() (or copy())? i think this maybe useful
> l1 = [1, 2, 3]
> l2 = l1[:]
> l3 = list(l1)
> Python-ideas mailing list
> Python-ideas at python.org
Yes, I plan to ask for copy() as well, when the bug tracker opens up
for 3.3, 3.4, etc. The issue is not, "Is there already a way to do
this?" but rather, "Can we have consistent interfaces in the sequence
types and collections where possible and appropriate?"
dict() and set() already support both clear() and copy() methods.
Previous posters have pointed to the disconnect and showed the problem
of having to test if a given iterable supports the clear() method
before calling it, in functions that take any iterable.
Also, for what it's worth:
s1 = set()
s2 = s1.copy()
is faster than
s1 = set()
s2 = set(s1)
(and also for dict()) probably because the first is
specifically-written for the copy operation whereas the second
actually iterates over s1, one item at a time. (At least I think
that's what's going on). I suppose that a list().copy() method might
also be faster than the other two approaches to copy a list.
Lastly, for completeness, I suppose copy() might be appropriate for
both tuple and deque as well.
More information about the Python-ideas