I want to append multiple elements to my list at once. I tried this
>>> l = []
>>> l.append('a')
>>> l
['a']
>>> l.append('b').append('c')
Traceback (most recent call last):
File "", line 1, in
l.append('b').append('c')
AttributeError: 'NoneType' object has no attribute 'append'
>>>
how can I append 'b' and 'c' at once?
解决方案
The method append() works in place. In other words, it modifies the list, and doesn't return a new one.
So, if l.append('b') doesn't return anything (in fact it returns None), you can't do:
l.append('b').append('c')
because it will be equivalent to
None.append('c')
Answering the question: how can I append 'b' and 'c' at once?
You can use extend() in the following way:
l.extend(('b', 'c'))