L[:]

A

Albert-Jan Roskam

In Python Cookbook, one of the authors (I forgot who) consistently used the"L[:]" idiom like below. If the second line simply starts with "L =" (sono "[:]") only the name "L" would be rebound, not the underlying object. That was the authorÅ› explanation as far as I can remember. I do not getthat. Why is the "L[:]" idiom more memory-efficient here? How could the increased efficiency be demonstrated?

#Python 2.7.3 (default, Sep 26 2013, 16:38:10) [GCC 4.7.2] on linux2
L = [x ** 2 for x in range(10)]
L[:] = ["foo_" + str(x) for x in L]


Thanks!


Regards,

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a

fresh water system, and public health, what have the Romans ever done for us?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top