>>> a = [1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
But:
>>> [1, 2, 3].append(4)
>>>
Why do list methods in Python (such as insert and append) only work with defined variables?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the second sample nothing is printed, because
append, that was called on a list (note that append was actually performed), returnsNone.Alternatively you should mention that
a.append(4)also gave you a blank line (as your first sample shows), and final output of a first code sample was a representation of result ofaexpression, nota.append('4')expression.Nothing is printed after
appendcall in both cases because it is a representation ofNone.