ary.extend (ext) merely adds reference to "ext" list to the end of the "ary" list, resulting in less memory transactions. As a result, .extend works orders of magnitude faster and doesn't use any additional memory outside of the list being extended and the list it's being extended with.
As we can see, extend with list comprehension is still over two times faster than appending. Generator expressions appear noticeably slower than list comprehension. append_comp only introduces unnecessary list creation overhead.
Append has (amortized) constant time complexity, O (1). Extend has time complexity, O (k). Iterating through the multiple calls to .append() adds to the complexity, making it equivalent to that of extend, and since extend's iteration is implemented in C, it will always be faster if you intend to append successive items from an iterable onto a list.
1522 There doesn't seem to be a way to extend an existing JavaScript array with another array, i.e. to emulate Python's extend method. I want to achieve the following:
In order to prevent having to specify all of the settings within the config object, I use jQuery's extend method to fill in a new object, settings with any default values from the default object if they weren't specified in the config object:
337 Extend is used when a use case adds steps to another first-class use case. For example, imagine "Withdraw Cash" is a use case of an Automated Teller Machine (ATM). "Assess Fee" would extend Withdraw Cash and describe the conditional "extension point" that is instantiated when the ATM user doesn't bank at the ATM's owning institution.
22 As others have pointed out, extend takes an iterable (such as a list, tuple or string), and adds each element of the iterable to the list one at a time, while append adds its argument to the end of the list as a single item. The key thing to note is that extend is a more efficient version of calling append multiple times.