Python list with initial size
Python - Create a list with initial capacityCode like this often happens: l = [] while foo: #baz l.append(bar) #quxThis is really slow if you're about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements. In Java, you can create an ArrayList with an initial capacity. If you have some idea how big your list will be, this will be a lot more efficient. I understand that code like this can often be re-factored into a list comprehension. If the for/while loop is very complicated, though, this is unfeasible. Is there any equivalent for us Python programmers? python list dictionary initialization bhargav 3 Years agoAnswers 9 Subscribe Submit Answer
View more answer
|