<p>Code like this often happens:</p>
<pre><code>l = []
while foo:
    # baz
    l.append(bar)
    # qux
</code></pre>
<p>This 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.</p>
<p>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.</p>
<p>I understand that code like this can often be refactored into a list comprehension. If the <em>for</em>/<em>while</em> loop is very complicated, though, this is unfeasible. Is there an equivalent for us Python programmers?</p>

Python lists have no built-in pre-allocation. If you really need to make a list, and need to avoid the overhead of appending (and you should verify that you do), you can do this:
```
```
l = [None] * 1000 # Make a list of 1000 None's
for i in xrange(1000):
    # baz
    l[i] = bar
    # qux
```
```
Perhaps you could avoid the list by using a generator instead:
```
```
def my_things():
    while foo:
        #baz
        yield bar
        #qux

for thing in my_things():
    # do something with thing
```
```
This way, the list isn't every stored all in memory at all, merely generated as needed.