What's the difference between the list methods append()
and extend()
?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I hope I can make a useful supplement to this question. If your list stores a specific type object, for example
Info
, here is a situation thatextend
method is not suitable: In afor
loop and and generating anInfo
object every time and usingextend
to store it into your list, it will fail. The exception is like below:But if you use the
append
method, the result is OK. Because every time using theextend
method, it will always treat it as a list or any other collection type, iterate it, and place it after the previous list. A specific object can not be iterated, obviously.append
adds its argument as a single element to the end of a list. The length of the list itself will increase by one.extend
iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.append
The
list.append
method appends an object to the end of the list.Whatever the object is, whether a number, a string, another list, or something else, it gets added onto the end of
my_list
as a single entry on the list.So keep in mind that a list is an object. If you append another list onto a list, the first list will be a single object at the end of the list (which may not be what you want):
extend
The
list.extend
method extends a list by appending elements from an iterable:So with extend, each element of the iterable gets appended onto the list. For example:
Keep in mind that a string is an iterable, so if you extend a list with a string, you'll append each character as you iterate over the string (which may not be what you want):
Operator Overload,
__add__
(+
) and__iadd__
(+=
)Both
+
and+=
operators are defined forlist
. They are semantically similar to extend.my_list + another_list
creates a third list in memory, so you can return the result of it, but it requires that the second iterable be a list.my_list += another_list
modifies the list in-place (it is the in-place operator, and lists are mutable objects, as we've seen) so it does not create a new list. It also works like extend, in that the second iterable can be any kind of iterable.Don't get confused -
my_list = my_list + another_list
is not equivalent to+=
- it gives you a brand new list assigned to my_list.Time Complexity
Append has 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.Performance
You may wonder what is more performant, since append can be used to achieve the same outcome as extend. The following functions do the same thing:
So let's time them:
Addressing a comment on timings
A commenter said:
Do the semantically correct thing. If you want to append all elements in an iterable, use
extend
. If you're just adding one element, useappend
.Ok, so let's create an experiment to see how this works out in time:
And we see that going out of our way to create an iterable just to use extend is a (minor) waste of time:
We learn from this that there's nothing gained from using
extend
when we have only one element to append.Also, these timings are not that important. I am just showing them to make the point that, in Python, doing the semantically correct thing is doing things the Right Way™.
It's conceivable that you might test timings on two comparable operations and get an ambiguous or inverse result. Just focus on doing the semantically correct thing.
Conclusion
We see that
extend
is semantically clearer, and that it can run much faster thanappend
, when you intend to append each element in an iterable to a list.If you only have a single element (not in an iterable) to add to the list, use
append
.An English dictionary define the words
append
andextend
as:append: add (something) to the end of a written document.
extend: make larger. Enlarge or expand
With that knowledge, now let's understand
1) The difference between
append
andextend
append
:extend
:list(iterable)
.2) Similarity between
append
andextend
None
.Example
extend(L) extends the list by appending all the items in the given list L.
An interesting point that has been hinted, but not explained, is that extend is faster than append. For any loop that has append inside should be considered to be replaced by list.extend(processed_elements).
Bear in mind that apprending new elements might result in the realloaction of the whole list to a better location in memory. If this is done several times because we are appending 1 element at a time, overall performance suffers. In this sense, list.extend is analogous to "".join(stringlist).
append(object)
- Updates the list by adding an object to the list.extend(list)
- Essentially concatenates two lists.