Solving problem is about exposing yourself to as many situations as possible like What is the difference between Python’s list methods append and extend? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about What is the difference between Python’s list methods append and extend?, which can be followed any time. Take easy to follow this discuss.
What’s the difference between the list methods
append: Appends object at the end.
x = [1, 2, 3] x.append([4, 5]) print (x)
[1, 2, 3, [4, 5]]
extend: Extends list by appending elements from the iterable.
x = [1, 2, 3] x.extend([4, 5]) print (x)
[1, 2, 3, 4, 5]
append adds an element to a list, and
extend concatenates the first list with another list (or another iterable, not necessarily a list.)
'a', 'b', 'mpilgrim', 'z', 'example'] li ['a', 'b', 'mpilgrim', 'z', 'example'] li.append("new") li ['a', 'b', 'mpilgrim', 'z', 'example', 'new'] li.append(["new", 2]) li ['a', 'b', 'mpilgrim', 'z', 'example', 'new', ['new', 2]] li.insert(2, "new") li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', ['new', 2]] li.extend(["two", "elements"]) li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', ['new', 2], 'two', 'elements']li = [
What is the difference between the list methods append and extend?
appendadds its argument as a single element to the end of a list. The length of the list itself will increase by one.
extenditerates 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.
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.
'foo', 'bar'] my_list.append('baz') my_list ['foo', 'bar', 'baz']my_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):
1, 2, 3] my_list.append(another_list) my_list ['foo', 'bar', 'baz', [1, 2, 3]] #^^^^^^^^^--- single item at the end of the list.another_list = [
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:
'foo', 'bar'] another_list = [1, 2, 3] my_list.extend(another_list) my_list ['foo', 'bar', 1, 2, 3]my_list [
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):
'baz') my_list ['foo', 'bar', 1, 2, 3, 'b', 'a', 'z']my_list.extend(
+= operators are defined for
list. 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.
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.
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:
def append(alist, iterable): for item in iterable: alist.append(item) def extend(alist, iterable): alist.extend(iterable)
So let’s time them:
import timeit min(timeit.repeat(lambda: append(, "abcdefghijklmnopqrstuvwxyz"))) 2.867846965789795 min(timeit.repeat(lambda: extend(, "abcdefghijklmnopqrstuvwxyz"))) 0.8060121536254883
Addressing a comment on timings
A commenter said:
Perfect answer, I just miss the timing of comparing adding only one element
Do the semantically correct thing. If you want to append all elements in an iterable, use
extend. If you’re just adding one element, use
Ok, so let’s create an experiment to see how this works out in time:
def append_one(a_list, element): a_list.append(element) def extend_one(a_list, element): """creating a new list is semantically the most direct way to create an iterable to give to extend""" a_list.extend([element]) import timeit
And we see that going out of our way to create an iterable just to use extend is a (minor) waste of time:
min(timeit.repeat(lambda: append_one(, 0))) 0.2082819009956438 min(timeit.repeat(lambda: extend_one(, 0))) 0.2397019260097295
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.
We see that
extend is semantically clearer, and that it can run much faster than
append, 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 appends a single element.
extend appends a list of elements.
Note that if you pass a list to append, it still adds one element:
1, 2, 3] a.append([4, 5, 6]) a [1, 2, 3, [4, 5, 6]]a = [
Append vs Extend
With append you can append a single element that will extend the list:
1,2] a.append(3) a [1,2,3]a = [
If you want to extend more than one element you should use extend, because you can only append one elment or one list of element:
4,5]) a [1,2,3,[4,5]]a.append([
So that you get a nested list
Instead with extend, you can extend a single element like this
1,2] a.extend() a [1,2,3]a = [
Or, differently, from append, extend more elements in one time without nesting the list into the original one (that’s the reason of the name extend)
4,5,6]) a [1,2,3,4,5,6]a.extend([
Adding one element with both methods
Both append and extend can add one element to the end of the list, though append is simpler.
append 1 element
1,2] x.append(3) x [1,2,3]x = [
extend one element
1,2] x.extend() x [1,2,3]x = [
Adding more elements… with different results
If you use append for more than one element, you have to pass a list of elements as arguments and you will obtain a NESTED list!
1,2] x.append([3,4]) x [1,2,[3,4]]x = [
With extend, instead, you pass a list as an argument, but you will obtain a list with the new element that is not nested in the old one.
1,2] z.extend([3,4]) z [1,2,3,4]z = [
So, with more elements, you will use extend to get a list with more items.
However, appending a list will not add more elements to the list, but one element that is a nested list as you can clearly see in the output of the code.
The following two snippets are semantically equivalent:
for item in iterator: a_list.append(item)
The latter may be faster as the loop is implemented in C.
append() method adds a single item to the end of the list.
x = [1, 2, 3] x.append([4, 5]) x.append('abc') print(x) # gives you [1, 2, 3, [4, 5], 'abc']
extend() method takes one argument, a list, and appends each of the items of the argument to the original list. (Lists are implemented as classes. “Creating” a list is really instantiating a class. As such, a list has methods that operate on it.)
x = [1, 2, 3] x.extend([4, 5]) x.extend('abc') print(x) # gives you [1, 2, 3, 4, 5, 'a', 'b', 'c']
From Dive Into Python.
You can use “+” for returning extend, instead of extending in place.
l1=range(10) l1+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11] l2=range(10,1,-1) l1+l2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2]
+= for in place behavior, but with slight differences from
extend. One of the biggest differences of
extend is when it is used in function scopes, see this blog post.