2
$\begingroup$

I was wondering if there is a reason behind clearing all the values in a list and reusing it, as opposed to deleting the list and creating a new one. Are there any practical advantages of using clear() method, aside from situations where the list is referenced in multiple locations, and deleting it might lead to unintended consequences?

$\endgroup$
2
  • $\begingroup$ I'll answer this question here, but it would also be appropriate on PLDI SE (langdev) since it is about the design and implementation of Python. $\endgroup$
    – Corbin
    Commented Apr 24 at 16:41
  • $\begingroup$ stackoverflow.com/q/77912023/781723 $\endgroup$
    – D.W.
    Commented Apr 24 at 17:53

2 Answers 2

1
$\begingroup$

In Python, list.clear() fulfills several goals simultaneously.

The question mentions one use: when a list is shared between multiple objects which are mutating it in tandem, .clear() can reset to a known-good shared state. This is common in work queues and can also arise from several sorts of federated state machines or distributed systems, particularly multi-user systems which have to reconcile input from multiple users.

A comment mentions another use. In CPython, the literal [] compiles to the bytecode BUILD_LIST 0, which boils down to a call to PyList_New(0). This means that creating a new empty list reliably performs a heap allocation; the C API requires the C-level handle to the new list to be distinct from other handles. So, .clear() can be used to recycle a list on the heap and avoid a heap allocation in CPython! This logic doesn't apply to any implementations with COW or JIT techniques, like PyPy.

Starting in Python 3.3, all mutable sequences were unified with a common API. The documentation notes that .clear() is paired with .copy(). This shows another purpose for .clear(): manual memory management. The programmer can choose whether to forget a reference to a list or forget every reference held by a list, giving fine-grained advice to the GC. This is useful in concurrent situations.

$\endgroup$
0
$\begingroup$

What do you mean by deleting a list? for example, if you go on another iteration in the loop, however you want cleared list and not with some random items from previous iteration. my answer is more useless, because you can use some other language features to achieve exactly same result. interesting discussion about list.clear() method can be found here: https://stackoverflow.com/questions/1401933/why-is-there-no-list-clear-method-in-python , notice that question was asked when list.clear() method wasn't added in python and updated discussion followed after it was added. https://bugs.python.org/issue10516 _ I think this will also be useful.

$\endgroup$
1
  • 1
    $\begingroup$ Deleting a list means if I really need to do list.clear() and use it again, I can use empty list and I can delete my list if memory or other resource optimizations are required $\endgroup$ Commented Nov 26, 2023 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.