Are there any drawback to copying objects like this in Python? a = [1,2] b = list(a)
Maybe this method of copying lists is the most readable, concise, and quick compared to list comprehensions or for loops with b.append(item), etc. http://henry.precheur.org/python/copy_list The below links also has some notes about the difference between b = list(a), b = a[:], and using some fancy copy module to copy the objects. http://stackoverflow.com/questions/2612802/how-to-clone-a-list-in-python http://stackoverflow.com/questions/184643/what-is-the-best-way-to-copy-a-list-in-python http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy
the first stackoverflow link has a good comparison summary at the end of the first answer where the object in the original list is updated and the copied lists that used methods like a[:] or list(a) share the same updated value of the original object in a, while the deep copied list (which contains entirely new objects instead of references to the same objects pointed to in the original list) still keeps the old value of the object.
Thanks
Join our real-time social learning platform and learn together with your friends!