0

I desire to create a copy of a list to perform some calculations, without affecting the original list. However, my code creates a named reference to the original list. Any changes I make to the new list are also applied to the old list. What am I doing wrong here?

tri = [[1],
    [2,8],
    [9,1,2]]

def tempR():
    ''' add up resulting triangle from RIGHT move '''
    # Generate triangle
    tempTri = tri.copy()
    del(tempTri[0])
    for r in range(len(tempTri)):
        del(tempTri[r][0])

    # Sum all the values
    pValue = 0
    for r in range(len(tempTri)):
        for d in tempTri[r]:
            pValue += d
    return(pValue)

tempR()
print(tri)
[[1], [8], [1, 2]]

I have tried both tempTri = list(tri) and tempTri = tri.copy() as suggested in this discussion.

What am I missing?

Jack
  • 242
  • 4
  • 18

0 Answers0