1

Alright so I know you cannot append a tuple to a list. However I am still receiving this error despite my best efforts. Can someone tell me what I am doing wrong or what is going on?

Traceback (most recent call last): File "C:/Users/.py", line 31, in listRow.append(convertedList) AttributeError: 'tuple' object has no attribute 'append'

followedBy is a string that comes from a cursor and is split into a list form. Below is some sample data that the cursor would contain.

followedBy = "0| 1| 2| 40"

table = [] #contains all rows (table)
row = [] #contains row
listFollowedBy = [] #contains ids 

for (var1, var2, var3, followedBy) in cursor:
   row = var1, var2, var3
   listFollowedBy = followedBy.split("| ") #Thought split always split the data into lists
   convertedList = list(listFollowedBy) #Threw this in there just to insure it was converted to a list
   row.append(convertedList)

   table.append(row)
6
  • 1
    Split does split as a list, you just can't keep track of your variables because they all have similar names. Commented Mar 12, 2015 at 1:32
  • Make sure you fix followed by. It looks like it should be a string. Commented Mar 12, 2015 at 1:35
  • row is a tuple as defined at the moment (you bind it to a list and then you rebind it to a tuple within your for suite). Change to row = [var1, var2, var3] Commented Mar 12, 2015 at 1:41
  • That was a typo in the question.This reflects more code more closely. Commented Mar 12, 2015 at 1:44
  • 1
    Row is a tuple! You're appending a list to a tuple. No can do! Commented Mar 12, 2015 at 1:45

1 Answer 1

2

Tuples are immutable meaning they can't be changed unless reassigned. You can however add tuples to a list because lists are mutable, meaning they can be changed. You need to edit the order of your append.

listFollowedBy.append(row)

As of now, your row is a tuple. Tuples don't have an append method, lists do. Why are you appending a list to a tuple instead of a tuple to a list? You've got the method call backwards.

Sign up to request clarification or add additional context in comments.

2 Comments

Its not a tuple its a list. Split should split it to a list. I also even converted it to a list just to double check
@user3586062 You don't understand. The append method is called on the list not the tuple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.