Error: Unsupported Operand Types
<p>I'm trying to use recursion to return the dot product of two lists, and I'm trying to account for the situation in which I get two lists of different length: I return 0. However, when I try to check for that condition, I get the error: unsupported operand type(s) for &amp;: 'list' and 'list'. Why can't I use the '&amp;' operand for two lists in Python?</p>

<pre><code>def dot(L, K):
    if L+K == []:
        return 0
    elif L == [] &amp; K != []:
        return 0
    elif K == [] &amp; L != []:
        return 0 
    else:
        return L[-1] * K[-1] + dot(L[:-1], K[:-1])
</code></pre>

<p>Your code is a bit more complicated than it really needs to be. It is not possible to take the dot product of two vectors which are not the same size. There are a couple of ways to deal with receiving vectors of different sizes.</p>

<p>1) Lop off the remaining unused numbers from the larger vector. Below is a modified version of your function. I changed it to only require one check for if either of the vectors is empty (there is no need to check this in multiple ways), and also changed it to start from the beginning of the vectors instead of the end. Was there a particular reason you started from the end?</p>

<pre><code>def dot(L, K):
    if(L == [] or K == []):
        return 0
    else:
        return L[0] + K[0] + dot(L[1:], K[1:])
</code></pre>

<p>While this option works, it does not give the user any indication that they made a mistake in attempting to dot product two different sized vectors.</p>

<p>2) Give the user an error upon receiving two different sized vectors.</p>

<pre><code>def dot(L, K):
    if(len(L) != len(K)):
        print('Vector sizes do not match, please pass two same-sized vectors')
        return 0 #not sure exactly how you are wanting to do error handling here.
    elif(L == [] or K == []):
        return 0
    else:
        return L[0] + K[0] + dot(L[1:], K[1:])
</code></pre>

python math