Saturday 15 May 2010

extend python list with a specified number -



extend python list with a specified number -

is there way extend list specified number? have list of size 5 , depending on homecoming value of function, need extend list size. code

list = [0,0] # size 2 size = some_function() # function returns 3 list.extend(size) print len(list) # should print 5

python doesn't have concept of 'pre-allocating' size lists. lists implemented arraylists , designed grow dynamically fixed size partitions. , index in list must contain value, there's no such thing allocated empty index (at to the lowest degree user has access to).

in cases, there little performance gain artificially inflating size of list before adding elements it. compounded fact lists lists of references, you're not preallocating memory particular object, bunch of pointers. if, however, that's want, following:

lst.extend([none]*size)

this grow list given size (note changed name of variable list lst not shadow builtin list), , fill slots none. however, speaking it's much improve extend when have items, or append them go.

python

No comments:

Post a Comment