JavaScript
x
10
10
1
>>> myList[1]
2
'from form'
3
>>> myList[1].append(s)
4
5
Traceback (most recent call last):
6
File "<pyshell#144>", line 1, in <module>
7
myList[1].append(s)
8
AttributeError: 'str' object has no attribute 'append'
9
>>>
10
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:
JavaScript
1
13
13
1
>>> myList
2
[1, 'from form', [1, 2, 't']]
3
>>> s = myList[1]
4
>>> s
5
'from form'
6
>>> s = [myList[1]]
7
>>> s
8
['from form']
9
>>> myList[1] = s
10
>>> myList
11
[1, ['from form'], [1, 2, 't']]
12
>>>
13
Advertisement
Answer
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.
JavaScript
1
14
14
1
>>> myList = [1, 'from form', [1,2]]
2
>>> myList[1]
3
'from form'
4
>>> myList[2]
5
[1, 2]
6
>>> myList[2].append('t')
7
>>> myList
8
[1, 'from form', [1, 2, 't']]
9
>>> myList[1].append('t')
10
Traceback (most recent call last):
11
File "<stdin>", line 1, in <module>
12
AttributeError: 'str' object has no attribute 'append'
13
>>>
14