extend python list with a specified number -
Is there a way to expand the list with a specified number? Initially I have a list of size 5 and based on the return value of some functions, I have to expand the list of that size. This is my code
list = [0,0] # size2 size = some_function () #function Returns 3 list. Extension (size) Print LAN (list) should print # 5
List of Python There is no concept of 'pre-allocation' size. The lists are applied in the form of arraylists and are dynamically developed from the partitions of fixed sizes. And there should always be a value in an index in the list, there is no such thing as allocated but empty index (at least that user has access).
In most cases, there will be very little performance. Before artificially adding elements, artificially increasing the size of the list is complicated by the fact that the lists are given a list of references, You are not able to undo memory for special objects, for only one sign, if you really want, you can do the following:
lst.extend ([ None] * size)
This will increase the size given In the list (note I changed the name of the variable from list
to lst
so that the bistin list
is not shadowed) and none < Fill the slot with / code> However, generally it says that when you have items, or when you go, make it better to add them.
Comments
Post a Comment