Difference between append vs. extend list methods in Python

Claudiu Source

What's the difference between the list methods append() and extend()?



answered 9 years ago Greg Hewgill #1

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:

>>> a = [1, 2, 3]
>>> a.append([4, 5, 6])
>>> a
[1, 2, 3, [4, 5, 6]]

answered 9 years ago Harley Holcombe #2

append adds an element to a list, extend concatenates the first list with another list (or another iterable, not necessarily a list.)

>>> li = ['a', 'b', 'mpilgrim', 'z', 'example']
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']

>>> li.append("new")               
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']

>>> li.insert(2, "new")            
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']

>>> li.extend(["two", "elements"]) 
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

From Dive into Python.

answered 9 years ago kender #3

append: Appends object at end.

x = [1, 2, 3]
x.append([4, 5])
print (x)

gives you: [1, 2, 3, [4, 5]]

extend: Extends list by appending elements from the iterable.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

gives you: [1, 2, 3, 4, 5]

answered 6 years ago Erik #4

The following two snippets are semantically equivalent:

for item in iterator:



The latter may be faster as the loop is implemented in C.

answered 5 years ago octoback #5

extend() can be used with an iterator argument. Here is an example. You wish to make a list out of a list of lists this way:


list2d = [[1,2,3],[4,5,6], [7], [8,9]]

you want

[1, 2, 3, 4, 5, 6, 7, 8, 9]

You may use itertools.chain.from_iterable() to do so. This method's output is an iterator. It's implementation is equivalent to

def from_iterable(iterables):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

Back to our example, we can do

import itertools
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
merged = list(itertools.chain.from_iterable(list2d))

and get the wanted list.

Here is how equivalently extend() can be used with an iterator argument:

merged = []
[1, 2, 3, 4, 5, 6, 7, 8, 9]

answered 5 years ago Chaitanya #6

append(object) - Updates the list by adding an object to the list.

x = [20]
# list passed to the append(object) method is treated as a single object.
#hence the resultant list length will be 2
print x 
--> [20, [21,22,23]]

extend(list) - Essentially concatenates 2 lists.

x = [20]
#the parameter passed to extend(list) method is treated as a list.
#eventually it is 2 list's being concatenated. 
#here the resultant list's length is 4
print x 

answered 5 years ago denfromufa #7

You can use "+" for returning extend, instead of extending in place.



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2]

Similarly += for in place behavior, but with slight differences from append & extend. One of the biggest differences of += from append and extend is when it is used in function scopes, see this blog post.

answered 4 years ago CodyChan #8

The append() method adds a single item to the end of the list.

x = [1, 2, 3]
x.append([4, 5])
print x
# gives you
[1, 2, 3, [4, 5], 'abc']

The 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])
print x
# gives you
[1, 2, 3, 4, 5, 'a', 'b', 'c']

From Dive Into Python

answered 4 years ago skdev75 #9

This is the equivalent of append and extend using the + operator:

>>> x = [1,2,3]
>>> x
[1, 2, 3]
>>> x = x + [4,5,6] # Extend
>>> x
[1, 2, 3, 4, 5, 6]
>>> x = x + [[7,8]] # Append
>>> x
[1, 2, 3, 4, 5, 6, [7, 8]]

answered 4 years ago bconstanzo #10

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).

answered 3 years ago Neeraj Sharma #11

list1 = [1,2,3,4,5]

list2 = ["a","b","c","d","e"]


print list.append(list2)

output : [1,2,3,4,5,["a","b","c","d","e"]]

extend :

print list1.extend(list2)

output : [1,2,3,4,5,"a","b","c","d","e"]

answered 3 years ago Shiv #12

Append adds the entire data at once. The whole data will be added to the newly created index. On the other hand, extend, as it name suggests, extends the current array.

For example

list1 = [123, 456, 678]
list2 = [111, 222]

With append we get:

result = [123, 456, 678, [111, 222]]

While on extend we get:

result = [123, 456, 678, 111, 222]

answered 3 years ago Aaron Hall #13

What is the difference between the list methods append and extend?

  • 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.


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.

>>> my_list
['foo', 'bar']
>>> my_list.append('baz')
>>> my_list
['foo', 'bar', 'baz']

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):

>>> another_list = [1, 2, 3]
>>> my_list.append(another_list)
>>> my_list
['foo', 'bar', 'baz', [1, 2, 3]]
                     #^^^^^^^^^--- single item on end of list.


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:

>>> my_list
['foo', 'bar']
>>> another_list = [1, 2, 3]
>>> my_list.extend(another_list)
>>> my_list
['foo', 'bar', 1, 2, 3]

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):

>>> my_list.extend('baz')
>>> my_list
['foo', 'bar', 1, 2, 3, 'b', 'a', 'z']

Operator Overload, __add__, (+) and __iadd__ (+=)

Both + and += 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.

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.


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:

def extend(alist, iterable):

So let's time them:

import timeit

>>> min(timeit.repeat(lambda: append([], "abcdefghijklmnopqrstuvwxyz")))
>>> min(timeit.repeat(lambda: extend([], "abcdefghijklmnopqrstuvwxyz")))

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 append.

Ok, so let's create an experiment to see how this works out in time:

def append_one(a_list, 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"""

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)))
>>> min(timeit.repeat(lambda: extend_one([], 0)))

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.

answered 3 years ago Sanyal #14

Append a dictionary to another one:
>>>def asd():
    dic = {1:'a',2:'b',3:'c',4:'a'}
    for i in dic.keys():
        if not newdic.has_key(dic[i]):
            newdic[i] = dic[i]
    print "appended one:",newdic

appended one: {1: 'a', 2: 'b', 3: 'c', 4: 'a', 5: 'v'}

answered 2 years ago The Gr8 Adakron #15

append(): It is basically used in Python to add one element.

Example 1:

>> a = [1, 2, 3, 4]
>> a.append(5)
>> print(a)
>> a = [1, 2, 3, 4, 5]

Example 2:

>> a = [1, 2, 3, 4]
>> a.append([5, 6])
>> print(a)
>> a = [1, 2, 3, 4, [5, 6]]

extend(): Where extend(), is used to merge two lists or insert multiple elements in one list.

Example 1:

>> a = [1, 2, 3, 4]
>> b = [5, 6, 7, 8]
>> a.extend(b)
>> print(a)
>> a = [1, 2, 3, 4, 5, 6, 7, 8]

Example 2:

>> a = [1, 2, 3, 4]
>> a.extend([5, 6])
>> print(a)
>> a = [1, 2, 3, 4, 5, 6]

answered 2 years ago Viraj Kaulkar #16

The method "append" adds its parameter as a single element to the list, while "extend" gets a list and adds its content.

for example,


    letters = ['a', 'b']  
    letters.extend(['c', 'd'])   
    print(letters)   # ['a', 'b', 'c', 'd']


    letters.append(['e', 'f'])   
    print(letters)    # ['a', 'b', 'c', 'd', ['e', 'f']]

answered 2 years ago tes #17

extend(L) extends the list by appending all the items in the given list L.

 >>> a
[1, 2, 3]
a.extend([4]) # it is equivalent to a[len(a):] = [4] or a.append(4)
[1, 2, 3, 4]
 >>> a = [1
 >>> a
[1, 2, 3]
 >>> a[len(a)] = [4]
 >>> a
[1, 2, 3, 4]

answered 1 year ago Crabime #18

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 that extend method is not suitable: In a for loop and and generating an Info object every time and using extend to store it into your list, it will fail. The exception is like below:

TypeError: 'Info' object is not iterable

But if you use the append method, the result is OK. Because every time using the extend 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.

answered 5 months ago Giovanni Gianni #19

Append vs Extend

enter image description here

With append you can append a single element that will extend the list:

>>> a = [1,2]
>>> a.append(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:

>>> a.append([4,5])
>>> a
>>> [1,2,3,[4,5]]

So that you get a nested list

Instead with extend you can extend a single element like this

>>> a = [1,2]
>>> a.extend([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)

>>> a.extend([4,5,6])
>>> a

Adding one element with both methods

enter image description here

append 1 element

>>> x = [1,2]
>>> x.append(3)
>>> x

extend one element

>>> x = [1,2]
>>> x.extend([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!

>>> x = [1,2]
>>> x.append([3,4])
>>> x

With extend, instead, you pass a list as argument, but you will obtain a list with the new element that are not nested in the old one.

>>> z = [1,2] 
>>> z.extend([3,4])
>>> z

So, with more elements, you will use extend to get a list with more items. You will use append, to append not more elements to the list, but one element that is a nested list as you can clearly see in the output of the code.

enter image description here

enter image description here

answered 3 months ago Yumi Tada #20

To distinguish them intuitively

l1 = ['a', 'b', 'c']
l2 = ['d', 'e', 'f']
In [16]: l1.append(l2)
In [17]: l1
Out[17]: ['a', 'b', 'c', ['d', 'e', 'f']]

It's like l1 reproduce a body within her body.

#reset l1 = ['a', 'b', 'c']
In [36]: l1.extend(l2)
In [37]: l1
Out[37]: ['a', 'b', 'c', 'd', 'e', 'f']

It's like that two Separated individuals get married and construct an united family.

Besides I make an exhaustive cheatsheet of all list's methods for your reference.

list_methods = {'Add': {'extend', 'append', 'insert'},
                'Subtract': {'pop', 'remove'}
                'Sort': {'reverse', 'sort'},
                'Search': {'count', 'index'},
                'Entire': {'clear', 'copy'},

answered 3 months ago kmario23 #21

English dictionary define the words append and extend 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) difference between append and extend


  • appends any Python object as-is to the end of the list (i.e. as a last element in the list).
  • resulting list may be nested and contain heterogeneous elements (i.e. list, string, tuple, dictionary, set etc.)


  • accepts any iterable as its argument and makes the list larger
  • resulting list is always one dimensional list (i.e. no nesting) and it may contain heterogeneous elements in it (e.g. characters, integers, float) as a result of applying list(iterable)

2) similarity between append and extend

  • Both takes exactly one argument
  • Both modify the list in-place
  • As a result, both returns None


lis = [1, 2, 3]

# extend is equivalent to this
lis = lis + list(iterable)

# append simply appends its argument as the last element to the list
# as long as the argument is a valid Python object

comments powered by Disqus