Trying to implement a simple edit distance module
<p>This is the code for the function :</p>

<pre><code>def populateConfusionMatrix(word,errword):
    dp = [[0]*(len(errword)+1) for i in range(len(word)+1)]
    m = len(word)+1;
    n = len(errword)+1;
    for i in range(m):
        for j in range(n):
            dp[i][0] = i;
            dp[0][j] = j;
    for i in range(m):
        for j in range(n): 
            print(i,j)
            if i==0 or j==0 :
                continue

            dis = [0]*4
            dis[0] = dp[i-1][j]+1
            dis[1] = dp[i][j-1]+1
            print("dis[1] is ",dp[i][j-1]+1)
            if word[i-1] == errword[j-1]:
                dis[2] = dp[i-1][j-1]
            else :
                dis[2] = dp[i-1][j-1]+1

            if i&gt;1 and j&gt;1 and word[i] == errword[j-1] and word[i-1] == errword[j]:
                dis[3] = dp[i-2][j-2] + 1 
            if dis[3]!=0 :
                dp[i][j] = min(dp[0:4])
            else :
                dp[i][j] = min(dp[0:3])

    i = m
    j = n
    while(i&gt;=0 and j&gt;=0) :
        if word[i-1] == errword[j-1] :
            i=i-1
            j=j-1
            continue
        if dp[i][j] == dp[i][j-1]+1 :
            populate_ins(word[i],errword[j])
            j=j-1
        if dp[i][j] == dp[i-1][j]+1 :
            populate_del(errword[j],word[i])
            i=i-1
        if dp[i][j] == dp[i-1][j-1] + 1 :
            populate_sub(word[i],errword[j])
            i=i-1
            j=j-1
        if i&gt;1 and j&gt;1 and word[i] == errword[j-1] and word[i-1] == errword[j] and dp[i][j] == dp[i-2][j-2]+1 :
            populate_exc(word[i-1],word[i])
            i=i-1
            j=j-1
</code></pre>

<p>But this code is showing this error on calling the function:</p>

<pre><code>populateConfusionMatrix("actress","acress")
</code></pre>

<p>Error - </p>

<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-25-d5ba10f95b61&gt; in &lt;module&gt;()
----&gt; 1 populateConfusionMatrix("actress","acress")

&lt;ipython-input-24-e996de70e204&gt; in populateConfusionMatrix(word, errword)
     15             dis = [0]*4
     16             dis[0] = dp[i-1][j]+1
---&gt; 17             dis[1] = dp[i][j-1]+1
     18             print("dis[1] is ",dp[i][j-1]+1)
     19             if word[i-1] == errword[j-1]:

TypeError: can only concatenate list (not "int") to list
</code></pre>

<p>Tried to print till what value of (i,j) the loop is working fine, I got this - </p>

<pre><code>(0, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(0, 6)
(1, 0)
(1, 1)
('dis[1] is ', 2)
(1, 2)
</code></pre>

<p>Your code is really difficult to understand, but the problem is definitely in these lines:</p>

<pre><code>if dis[3]!=0 :
    dp[i][j] = min(dp[0:4])
else :
    dp[i][j] = min(dp[0:3])
</code></pre>

<p>Since your list <code>dp</code> has the value:</p>

<pre><code>[[0, 1, 2, 3, 4, 5, 6], [1, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0, 0], [4, 0, 0, 0, 0, 0, 0], [5, 0, 0, 0, 0, 0, 0], [6, 0, 0, 0, 0, 0, 0], [7, 0, 0, 0, 0, 0, 0]]
</code></pre>

<p>When you use <code>dp[i][j] = min(dp[0:3])</code>, you're calling <code>min</code> on a slice of <code>dp</code>, or:</p>

<pre><code>min([0, 1, 2, 3, 4, 5, 6], [1, 0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0, 0])
</code></pre>

<p>That's why you're getting errors later on while trying to add a number to a list:</p>

<pre><code>dis[1] = dp[i][j-1] + 1  # evaluates to something like [0,0,0,0] + 1
</code></pre>

python