What is the difference between:
some_list1 = [] some_list1.append('something')
and
some_list2 = [] some_list2 += ['something']
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.
For your case the only difference is performance: append is twice as fast.
In general case
appendwill add one item to the list, while+=will copy all elements of right-hand-side list into the left-hand-side list.Update: perf analysis
Comparing bytecodes we can assume that
appendversion wastes cycles inLOAD_ATTR+CALL_FUNCTION, and += version — inBUILD_LIST. ApparentlyBUILD_LISToutweighsLOAD_ATTR+CALL_FUNCTION.We can improve performance even more by removing
LOAD_ATTRoverhead: