'Python list, last element overwrites entire list

I am working on a program that reads logfiles in, in a weird format. I have the following code reading from a number of logfiles

(logFiles)

It works fine to itterate through them and so on. The problem is that for some reason the last element that i append to the list dataList overwrites the entire list so all fields are the same..

    def fetchDataFromLogFiles(colIndex, colNames, colType, logFiles):
    dataList = []
    rowchange = []
    row = []
    for i in range(0, len(colIndex)):
        row.append(0)
        rowchange.append(0)
    for x in range(0, len(logFiles)):
        print("fetching data from: ")
        print(logFiles[x])
        logFile = open(logFiles[x] , "r")
        for x in range(0,4):
            line = logFile.readline()
            if not line:
                break
            else:
                line = line.split(",")
                TimeStamp = line[1]
                rest = line[2:]
                
                
                #TimeDate formatting
                whiteSpaces = len(TimeStamp)- len(TimeStamp.lstrip())
                TimeStamp = TimeStamp[whiteSpaces:]
                TimeStampFormat = "%a %b %d %H:%M:%S %Y"
                DT = datetime.datetime.strptime(TimeStamp, TimeStampFormat)
                #print(type(DT))
                #print(DT)
                row[0] = DT
                rowchange[0] +=1
                
                for i in range(0, len(rest)):
                    rest[i] = rest[i].replace(" ","")
                    if(i%2 == 0):
                        try:
                            index = colIndex.index(rest[i])
                            rowchange[index] += 1
                            if(colType[index] == "Bool"):
                                if(rest[i+1] == "0"):
                                    row[index] = False
                                    #print(type(row[index]))
                                else:
                                    row[index] = True
                                    #print(type(row[index]))
                            elif(colType[index] == "Short"):
                                row[index] = int(rest[i+1])
                                #print(type(row[index]))
                            else:
                                row[index] = rest[i+1]
                            
                        except:
                            continue
            print(x)
            print(row[0])
            dataList.append(row)
            print(dataList[x][0])
        logFile.close()
        print("file closed")
    for x in range (0,len(dataList)):
        print(dataList[x][0])
    return dataList  

Debug print output as follows to show what i mean

0
2022-01-24 04:57:39
2022-01-24 04:57:39
1
2022-01-24 04:57:39
2022-01-24 04:57:39
2
2022-01-24 04:57:39
2022-01-24 04:57:39
3
2022-01-24 04:57:40
2022-01-24 04:57:40
file closed
2022-01-24 04:57:40
2022-01-24 04:57:40
2022-01-24 04:57:40
2022-01-24 04:57:40

A couple of lines of log file:

$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1,    #0, 42,   #1, 130,   #2, 144,    #3, 49,    #4, 45,    #5, 66,  #6, 2975, #7, -3981,  #8, 4028,  #9, 3900, #10, 2480, #13, 4029, #16, 2930, #17, 2190,  #20, 102,    #21, 2, #24, 2900, #25, 2200,    #26, 6,   #27, 51, #30, 2898,    #31, 0,    #32, 0,  #33, 150,  #34, 511,   #35, -2,   #36, 22,  #37, 549,   #38, -2,   #39, 22,   #40, 60,   #41, 45,  #42, 218, #43, -306,  #45, 236, #46, -152,  #48, 100,    #49, 0,  #50, 100,    #51, 0,  #52, 137, #53, -200,  #55, 137,  #56, -64, #58, 7850,    #59, 1, #60, 8300,    #61, 1,  #62, 100,   #63, -1,   #64, 60,   #65, 88,   #66, 86,  #67, 108,    #68, 1, #73, 1800, #74, 2500,    #76, 0,    #77, 0,    #78, 0,    #79, 0,    #80, 0,  #81, 173,  #82, 174,    #83, 0,    #84, 0,  #85, -11, #86, -100,  #88, 400, #89, 2729,    #90, 0, #91, 2762,    #93, 5,   #94, 15,   #95, 11,   #96, 99,   #97, 30,    #98, 0,   #100, 0,   #101, 0,   #102, 0,#105, 1252,   #106, 0,   #107, 0,   #108, 0,#109, 4029,   #110, 0,#111, 3900,#112, 2480,#113, 2520,#114, 2200,#115, 2900,   #116, 1,   #117, 0,   #118, 0, #119, 156, #120, 165,   #121, 0,   #123, 0,   #124, 0,  #126, 40,  #127, 60, #128, 350, #129, 450,  #130, 57,#131, 1740,#132, 1682,   #133, 1,   #134, 0,   #135, 0,   #136, 0,   #137, 2,   #138, 0, #141, 135,   #142, 0,#150, 4800,   #151, 0,   #152, 0,#153, 4864,   #154, 1,   #155, 1,   #156, 0,   #157, 0,   #158, 1,  #160, 15,  #161, 40,  #162, 15,  #163, 40,   #164, 1,   #165, 1,   #166, 0,#170, 1000,#172, 3000,   #173, 0,   #175, 0,   #179, 0, #180, 599,   #181, 0,   #182, 9,  #183, 42,   #184, 0,   #185, 7,  #186, 49,   #187, 0,   #188, 4,   #189, 1,   #190, 1,#191, 2261,#192, 1796,   #193, 1,   #194, 0,   #197, 0,#199, 2000,#200, 10000,  #201, 10,  #202, 10,#203, 3000,   #204, 0, #205, 900,   #206, 0,#209, 1774, #210, 300, #211, 900,#212, 1800, #213, 100,#217, 3000,#218, 4500,#219, 1500,#220, 3000,  #222, 60,   #223, 9,#224, 3150,   #225, 0,   #226, 0,  #231, 30,   #232, 1,   #233, 0,   #234, 0, #235, 100,   #236, 0,   #237, 0,  #240, 34,  #241, 29,   #251, 0,
$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1,  #8, 4029, #30, 2897,  #82, 173, #141, 132,#153, 5120,   #185, 8,
$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1, #30, 2898, #91, 2761,  #130, 17,#131, 1701,#132, 1683, #141, 130,#153, 5376,   #155, 0,   #185, 9,
$1,      Mon Jan 24 04:57:40 2022,$2,10,$3,42,$4, 0,$5, 1, #7, -3982,  #8, 4028, #89, 2731, #141, 128,#153, 5632,   #185, 8,

After Ethans comment i edited the code to not use globals, but still have the same problem



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source