Home > Software design >  Python 'str' object has no attribute 'append' even when operating on list? [dupl
Python 'str' object has no attribute 'append' even when operating on list? [dupl

Time:10-01

I keep getting this simple error which I can't see why. I'm trying to remove duplicate letters at same index in multiple plaintext but I get this letter which doesnt seem logical since l is an array of smaller arrays and these smaller arrays have a list as an element to say what unique letters found at this specific index. Note that the print return the list as supposed to be.

n = []
l = []
for x in range(83):
    l.append( [-1, [] ] )
print(l[0][1])
for x in solved_letters:
    if x[0] not in n:
        n.append(x[0])
        l[x[0]][0], l[x[0]][1] = x[0], x[1]
    else:
        for v in x[1]:
            if v not in l[x[0]][1]:
                l[x[0]][1].append(v)

sample of solved_letters

[[30, 'a'], [32, 'w'], [34, ' '], [36, 's'], [42, 'n'], [45, 'p'], ...]

CodePudding user response:

The problem is that solved_letters second elements (i.e. solved_letters[*][1]) are letters and not lists so once you assign them in l in the following line - l[x[0]][0], l[x[0]][1] = x[0], x[1], l will contain something line - l[30][30, 'a']. The next time you try to access it with the loop (i.e. for v in x[1]) you can iterate over a string but you cannot append since it is a string and not a list.

To overcome that I would change - l[x[0]][0], l[x[0]][1] = x[0], x[1] to - l[x[0]][0], l[x[0]][1] = x[0], [x[1]].

  • Related