The following line may well cause problems:
final=final.append(A)
Remove the "final=" thus:
final.append(A)
Also you should add some print statements after each
final.append
statement, to see what the results are:
print(F"{final=}")
That will give you some nice debug output.