How to fix “AttributeError: ‘str’ object has no attribute ‘append'”

Each Answer to this Q is separated by one/two green lines.

>>> myList[1]
'from form'
>>> myList[1].append(s)

Traceback (most recent call last):
  File "<pyshell#144>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

Why is myList[1] considered a 'str' object? mList[1] returns the first item in the list 'from form' but I cannot append to item 1 in the list myList.

I need to have a list of lists; so ‘from form’ should be a list. I did this:

>>> myList
[1, 'from form', [1, 2, 't']]
>>> s = myList[1]
>>> s
'from form'
>>> s = [myList[1]]
>>> s
['from form']
>>> myList[1] = s
>>> myList
[1, ['from form'], [1, 2, 't']]

myList[1] is an element of myList and its type is string.

myList[1] is a string, you can not append to it. myList is a list, you should have been appending to it.

>>> myList = [1, 'from form', [1,2]]
>>> myList[1]
'from form'
>>> myList[2]
[1, 2]
>>> myList[2].append('t')
>>> myList
[1, 'from form', [1, 2, 't']]
>>> myList[1].append('t')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

If you want to append a value to myList, use myList.append(s).

Strings are immutable — you can’t append to them.

Why myList[1] is considered a ‘str’ object?

Because it is a string. What else is 'from form', if not a string? (Actually, strings are sequences too, i.e. they can be indexed, sliced, iterated, etc. as well – but that’s part of the str class and doesn’t make it a list or something).

mList[1] returns the first item in the list 'from form'

If you mean that myList is 'from form', no it’s not!!! The second (indexing starts at 0) element is 'from form'. That’s a BIG difference. It’s the difference between a house and a person.

Also, myList doesn’t have to be a list from your short code sample – it could be anything that accepts 1 as index – a dict with 1 as index, a list, a tuple, most other sequences, etc. But that’s irrelevant.

but I cannot append to item 1 in the list myList

Of course not, because it’s a string and you can’t append to string. String are immutable. You can concatenate (as in, “there’s a new object that consists of these two”) strings. But you cannot append (as in, “this specific object now has this at the end”) to them.

What you are trying to do is add additional information to each item in the list that you already created so

    alist[ 'from form', 'stuff 2', 'stuff 3']

    for j in range( 0,len[alist]):
        temp= []
        temp.append(alist[j]) # alist[0] is 'from form' 
        temp.append('t') # slot for first piece of data 't'
        temp.append('-') # slot for second piece of data

    blist.append(temp)      # will be alist with 2 additional fields for extra stuff assocated with each item in alist  

This is a simple program showing append('t') to the list:


for i in range(1):


[['i', 'k'], 't']

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .