Python Slice Assignment Memory Usage
I read in a comment here on Stack Overflow that it is more memory efficient to do slice assignment when changing lists. For example, a[:] = [i + 6 for i in a] should be more memory efficient than a = [i + 6 for i in a] because the former replaces elements in the existing list, while the latter creates a new list and rebinds a to that new list, leaving the old a in memory until it can be garbage collected. Benchmarking the two for speed, the latter is slightly quicker: $ python -mtimeit -s 'a = [1, 2, 3]' 'a[:] = [i + 6 for i in a]' 1000000 loops, best of 3: 1.53 usec per loop $ python -mtimeit