On Sun, Dec 1, 2013 at 7:27 PM, spir <denis.s...@gmail.com> wrote: > On 12/01/2013 05:32 AM, Amit Saha wrote: >> >> Hello, >> >> I was told by someone (as a comment) that a code snippet such as this >> "would make Pythonistas talk my ear off about how evil the append()" >> function is: >> >>>>> mylist = [] >>>>> mylist.append(1) >> >> # a number of times over >> >> I have some ideas that on an append() the list's internal size >> increases (doubled?) since CPython over-allocates memory, and such. >> >> So, the question is: what is the alternative to growing a list when I >> have no idea what items or how many may be there? > > > Maybe you are confusing with catenating _strings_, rather than lists. > Python's concat is problematic because it is a binary operation. So, > catenating n bits makes n-1 operations, each with intermediate results, of > growing sizes, all thrown away except for the last one, the actual result. > If all of the bitN are strings: > bit1 + bit2 + bit3 + bit4 + bit5 > actually constructs: > bit1+bit2 > bit1+bit2+bit3 > bit1+bit2+bit3+bit4 > bit1+bit2+bit3+bit4+bit5 > A number of unneeded string object, and a very big useless memory weight.
Thanks Denis, good to know this is how it is done. I hadn't thought about it simply because, never have probably concatenated strings beyond the simple "adding a new line" to a string. Best, Amit. -- http://echorand.me _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor