.clear() for list not working - python
<p>I am writing some code that is supposed to find the prime factorization of numbers. The main function increments through numbers; I'm doing that because I want to use the code to conduct timing experiments. I don't mind it not being super-efficient, part of the project for me will be making it more efficient myself. It is also not yet totally complete (for example, it doesn't simplify the prime factorization). I have tested all of the functions except the main function and they have worked, so there isn't a problem with those. </p>

<p>My code is </p>

<pre><code>import math 
import time

primfac=[]

def primes(n):
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]


def factfind(lsp,n): #finds factors of n among primes
    for i in lsp:
        if n%i==0:
            primfac.append(i)
        else:
            i+=1

def primfacfind(n1,n2):
    while n1 &lt; n2:
        n = n1

        time_start = time.clock()

        factfind(primes(n),n)
        print primfac

        time_elapsed = time.clock() - time_start
        print "time:", time_elapsed

        primfac.clear()

        n1+=1

print primfacfind(6,15)
</code></pre>

<p>And running it gives the output </p>

<pre><code>[2, 3]
time: 7.5e-05
Traceback (most recent call last):
  File "python", line 43, in &lt;module&gt;
  File "python", line 39, in primfacfind
AttributeError: 'list' object has no attribute 'clear'
</code></pre>

<p>And I'm not really sure what is wrong. It is giving the right numbers for the prime factorization and it is printing the time, but it can't seem to clear the list. Commenting out the line <code>primfac.clear()</code> has it working.</p>

<p>Any help would be appreciated. Thanks!</p>

<p>Python's <code>list</code> does not have a <code>clear</code> method until Python 3.x. Your code will not work in Python 2.x or earlier. You can either create a new list or delete all the contents of the old list.</p>

<pre><code>#create a new list 
primfac = []

#or delete all the contents of the old list
del primfac [:]
</code></pre>

python list runtime-error