Paul McGuire wrote:
Pythonistically speaking, even though a dict is a mutable thing, I'mI disagree. The convention is that mutation methods should return None.
learning that the accepted practice for methods like this is not so
much to update in place as it is to use generator expressions to
construct a new object.
For example, given a list of integers to 100,This is because each removal is O(n), making the entire process O(n*2),
instead of removing all of the even numbers (with the attendant
hassles of updating a list while iterating over it), just create a new
list of the numbers that are not even.
whereas a new list is O(n) -- and the hassle of remembering to iterate
in reverse when doing removals.
Perhaps I am overgeneralizing from comments I have read here on c.l.pyI think so. Removals and insertions are importantly different from
about creating new lists instead updating old ones.
change in place. If I wanted to square each number in a list, and *did
not need the original list*, I would not hesitate to do it in place.
The newish sorted() and reversed() built-ins were meant to complement
list.sort and list.reverse, not replace them.
tjr