{"id":12022,"date":"2022-02-26T13:19:58","date_gmt":"2022-02-26T17:19:58","guid":{"rendered":"http:\/\/nycphantom.com\/journal\/?p=12022"},"modified":"2022-02-26T13:34:31","modified_gmt":"2022-02-26T17:34:31","slug":"python-add-vs-inplace_add-vs-extend-for-list","status":"publish","type":"post","link":"http:\/\/nycphantom.com\/journal\/?p=12022","title":{"rendered":"Python: ADD (+) vs. INPLACE_ADD (+=) vs. extend() for LIST"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today's Python series by Jake Gober led me to consider why there's <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/3653298\/concatenating-two-lists-difference-between-and-extend\" target=\"_blank\">difference <\/a>between concatenate (+) two lists to the same variable and using the extend() function on the list.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-13.png\"><img loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"331\" src=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-13.png\" alt=\"\" class=\"wp-image-12024\" srcset=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-13.png 502w, http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-13-300x198.png 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The answer is ADD (+) creates a new list, I supposed even though the variable name is the same, it is still going to be a new list, so the list that was earlier assigned to the dictionary would not be affected. While += works on the same object, as does extend() rather than creating a new one, so there's no need to update the dictionary after the change in list. Therefore, ADD, by creating a new list, makes the operation slower, some clocked it 3 times slower than the other two, see chart below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I had to google it for a while because most results are only talking about the difference in runtime. But <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-concatenation-add-vs-inplace-add-vs-extend\/\" target=\"_blank\">this site<\/a> hits the mark. I've pasted the answer in the comment in case the site expires. They have a nice chart for runtime as well:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-14.png\"><img loading=\"lazy\" decoding=\"async\" width=\"599\" height=\"458\" src=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-14.png\" alt=\"\" class=\"wp-image-12025\" srcset=\"http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-14.png 599w, http:\/\/nycphantom.com\/journal\/wp-content\/uploads\/2022\/02\/image-14-300x229.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion: It seems that Python is using some kind of pointers in its object creation, but I am not certain and I fear it would take up more time and energy than I wanted to explore into it, so I'll leave it as a question for now. This is good enough.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today's Python series by Jake Gober led me to consider why there's difference between concatenate (+) two lists to the same variable and using the extend() function on the list. The answer is ADD (+) creates a new list, I &hellip; <a href=\"http:\/\/nycphantom.com\/journal\/?p=12022\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-12022","post","type-post","status-publish","format-standard","hentry","category-questions","category-technical"],"_links":{"self":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/12022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12022"}],"version-history":[{"count":6,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/12022\/revisions"}],"predecessor-version":[{"id":12033,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/12022\/revisions\/12033"}],"wp:attachment":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12022"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}