3112

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

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Claudiu
  • 224,032
  • 165
  • 485
  • 680

20 Answers20

5841

.append() appends a specified object at the end of the list:

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

.extend() extends the list by appending elements from the specified iterable:

>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]
blackraven
  • 5,284
  • 7
  • 19
  • 45
kender
  • 85,663
  • 26
  • 103
  • 145
  • 170
    What is the difference between `extend` and simply using the addition operator - in the above example, `x = x + [4, 5]`? – Rohan May 29 '17 at 22:10
  • 408
    Actually there's a ***big difference*** - `x + [4, 5]` gives you a new list assigned to x - `x.extend()` mutates the original list. I elaborate in my answer here below. – Russia Must Remove Putin Jul 17 '17 at 17:14
  • 10
    @AaronHall @Rohan but it is same as `x += [4,5]`. – Astitva Srivastava Dec 13 '18 at 14:54
  • 2
    The keyword when using `append` is **Object**. If you try to use `extend` and you pass in a **dictionary**, it will append the **key**, and not the whole hash to the end of the array. – Anthony Mar 15 '19 at 14:53
  • 1
    @Rohan, the time complexity of x = x + [4, 5] would be O(len(x) + len([4,5])) where as extend has the time complexity of O(len([4, 5])) – mcagriardic Jul 22 '20 at 16:20
  • `x += [4,5]` is the same as `extend`, and `x += [[4,5]]` behaves like `append`. – C-3PO Nov 21 '22 at 22:04
  • Is there a benefit of .append above += ? To clarify, I have some code that usually appends a single element to a given list, but occasionaly extends with a list. For the sake of brevity, I consider putting both operations in a single function, inventory += item. Any arguments against that? – Willem van Houten Jan 05 '23 at 08:44
732

.append() adds an element to a list,
whereas .extend() concatenates the first list with another list/iterable.

>>> xs = ['A', 'B']
>>> xs
['A', 'B']

>>> xs.append("D")
>>> xs
['A', 'B', 'D']

>>> xs.append(["E", "F"])
>>> xs
['A', 'B', 'D', ['E', 'F']]

>>> xs.insert(2, "C")
>>> xs
['A', 'B', 'C', 'D', ['E', 'F']]

>>> xs.extend(["G", "H"])
>>> xs
['A', 'B', 'C', 'D', ['E', 'F'], 'G', 'H']
blackraven
  • 5,284
  • 7
  • 19
  • 45
Harley Holcombe
  • 175,848
  • 15
  • 70
  • 63
650

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.

.append()

The .append() method appends an object to the end of the list.

my_list.append(object) 

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 at the end of the list.

.extend()

The .extend() method extends a list by appending elements from an iterable:

my_list.extend(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 (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.

Regarding "amortized" - from the list object implementation source:

    /* This over-allocates proportional to the list size, making room
     * for additional growth.  The over-allocation is mild, but is
     * enough to give linear-time amortized behavior over a long
     * sequence of appends() in the presence of a poorly-performing
     * system realloc().

This means that we get the benefits of a larger than needed memory reallocation up front, but we may pay for it on the next marginal reallocation with an even larger one. Total time for all appends is linear at O(n), and that time allocated per append, becomes O(1).

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:

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

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.

Conclusion

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

blackraven
  • 5,284
  • 7
  • 19
  • 45
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 1
    @Aaron Hall One small comment in algorithm for timing. "extend_one" may return "slightly wrong" time because the creation of a list is also involved. Probably better is to create the items as variables (`ex1 = 0` and `ex2 = [0]`) and pass these variables, if you want to be more strict. – ilias iliadis Mar 31 '18 at 18:06
  • 22
    Perfect answer indeed. What about performance of `l1 += l2` vs `l1.extend(l2)`? – Jean-Francois T. Apr 23 '18 at 03:54
  • 12
    @Jean-FrancoisT.: `l1 += l2` and `l1.extend(l2)` ultimately execute the same code (the `list_extend` function in `listobject.c`). The only differences are: 1. `+=` reassigns `l1` (to itself for `list`s, but the reassignment supports immutable types that aren't the same object after), which makes it illegal if `l1` is actually an attribute of an immutable object; for example, `t = ([],)`, `t[0] += lst` would fail, while `t[0].extend(lst)` would work. 2. `l1 += l2` uses dedicated bytecodes, while `l1.extend(l2)` uses generalized method dispatch; this makes `+=` faster than `extend`. – ShadowRanger Aug 17 '18 at 19:10
  • 4
    The fact that `+=` must reassign `l1` does mean that in some cases, the slower dispatch of `extend` is partially or wholly made up for by not assigning back to the left hand side. For example, if the `list` is an attribute of an object, `self.l1 += l2` and `self.l1.extend(l2)` have identical performance on my Python 3.6 install, simply because real operation is more like `self.l1 = self.l1.__iadd__(l2)`, which means it must perform a moderately expensive `STORE_ATTR` that `self.l1.extend(l2)` doesn't have to. – ShadowRanger Aug 17 '18 at 19:20
  • 3
    Simple comparison in local tests: For a local variable (so the `+=` is just using `STORE_FAST`, which is super cheap), where the value being added is an existing `list` with one item in it, with the operation repeated 1000 times, `+=` took about 33 ns on average, while `extend` took 78 ns, a difference of 45 ns. If `l1` is a global (requires more expensive `STORE_GLOBAL`), the difference narrows to 17 ns. If `l1` is actually `local.l1` (requires even more expensive `STORE_ATTR`), there is no meaningful difference between `+=` and `extend` (timings roughly identical; `extend` sometimes wins). – ShadowRanger Aug 17 '18 at 19:32
  • Could you clarify the difference in behavior between `my_list = my_list + another_list` and `my_list += another_list`? What's the practical difference between modifying an existing list and creating a new list to have identical contents and rebinding the name? – tparker Dec 22 '18 at 22:38
  • @tparker - if you have another name (foo) bound to my_list (say you are inside a function) then using += doesn't rebind my_list to a new object and therefore foo and my_list are still the same list. doing the explicit + and assignment (i.e. not using +=) means the RHS is a new list, and the name my_list is rebound, meaning that my_list and foo are no longer bound to the same object. – Tony Suffolk 66 Jun 26 '20 at 03:29
  • @Aaron Hall you said that `append` for `array a` takes O(1) time but what about the creation of a new array with size `sizeof(a)+1` with the desired values? (first items as in a and the last new item). I am really asking because I don't know how it works in python – Daniel Apr 13 '21 at 17:33
  • 1
    @Daniel I added some information to that section - which should lead you to more relevant information if you're interested. – Russia Must Remove Putin Apr 14 '21 at 00:14
  • A bit weird, "l1[0] += l2" fails but it extends the list, indeed (at least in my Python version) @ShadowRanger >>> try: ... l1 = ([], ) ... l1[0] += range(3) ... except TypeError: ... print('Exception got, but l1 is:', l1) ... Exception got, but l1 is: ([0, 1, 2],) – zk82 Nov 04 '21 at 11:02
  • 1
    @zk82: Yep. It completes the whole operation, then fails on the reassignment. Probably best to avoid it; I'd be forced to tar and feather anyone who actually used `+=` like that and just used a `try:`/`except TypeError: pass` to silence the exception. :-) – ShadowRanger Nov 04 '21 at 20:34
136

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]]
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
92

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
[1,2,3]

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
[1,2,3]

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
[1,2,3,4,5,6]

Adding one element with both methods

enter image description here

Both append and extend can add one element to the end of the list, though append is simpler.

append 1 element

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

extend one element

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

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
[1,2,[3,4]]

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.

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

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.

enter image description here

enter image description here

Clint Chelak
  • 232
  • 2
  • 9
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
67

The following two snippets are semantically equivalent:

for item in iterator:
    a_list.append(item)

and

a_list.extend(iterator)

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

Kenly
  • 24,317
  • 7
  • 44
  • 60
Nova
  • 2,623
  • 4
  • 26
  • 45
  • 22
    Extending is ~4x faster on my machine than appending in a loop (16us vs 4us for 100 loops of zeros) – Alex L Dec 27 '12 at 08:29
  • 6
    `extend()` probably preallocates, while `append()` likely does not. – Mad Physicist Oct 23 '15 at 13:43
  • @MadPhysicist: For completeness' sake, there would be times where `extend()` *can't* preallocate sensibly since some iterables don't implement `__len__()`, but like you I'd be surprised if it doesn't try. Some of the performance gain also comes from doing the iteration part in pure C instead of in Python, as pointed out in [Aaron's answer](https://stackoverflow.com/a/28119966/). – Soren Bjornstad Jul 28 '19 at 03:43
47

The 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']

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

From Dive Into Python.

Georgy
  • 12,464
  • 7
  • 65
  • 73
CodyChan
  • 1,776
  • 22
  • 35
  • You can't extend with just 6 since it's not iterable. And the second output in your example is wrong. 'abc' gets added as a single element since you passed it in to `extend` as a list with one element `['abc']`: [1, 2, 3, 4, 5, 'abc']. To make your example output correct, change the abc line to: `x.extend('abc')`. And remove the `x.extend(6)` or change it to `x.extend([6])`. – aneroid Sep 25 '14 at 09:28
  • Also "The extend() method takes one argument, a list," is wrong – am70 Feb 04 '22 at 09:51
41

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

l1=range(10)

l1+[11]

[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]

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.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
denfromufa
  • 5,610
  • 13
  • 81
  • 138
25

append(object) updates the list by adding the object to the list.

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

extend(list) concatenates the two lists essentially.

x = [20]
# The parameter passed to extend(list) method is treated as a list.
# Eventually it is two lists being concatenated.
x.extend([21, 22, 23])
# Here the resultant list's length is 4
print(x)
--> [20, 21, 22, 23]
blackraven
  • 5,284
  • 7
  • 19
  • 45
Chaitanya
  • 1,698
  • 5
  • 21
  • 41
23

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:

From

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. Its 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 = []
merged.extend(itertools.chain.from_iterable(list2d))
print(merged)
>>>
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kiriloff
  • 25,609
  • 37
  • 148
  • 229
23

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]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
skdev75
  • 720
  • 6
  • 8
18

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]
Bahrom
  • 4,752
  • 32
  • 41
The Gr8 Adakron
  • 1,200
  • 1
  • 12
  • 15
15

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

bconstanzo
  • 566
  • 4
  • 10
15

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]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shiv
  • 207
  • 2
  • 5
11

An English dictionary defines 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) The difference between append and extend

append:

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

extend:

  • Accepts any iterable as its argument and makes the list larger.
  • The 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 take exactly one argument.
  • Both modify the list in-place.
  • As a result, both returns None.

Example

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
list.append(object)
Gavriel Cohen
  • 4,355
  • 34
  • 39
kmario23
  • 57,311
  • 13
  • 161
  • 150
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Crabime
  • 644
  • 11
  • 27
5

To distinguish them intuitively

l1 = ['a', 'b', 'c']
l2 = ['d', 'e', 'f']
l1.append(l2)
l1
['a', 'b', 'c', ['d', 'e', 'f']]

It's like l1 reproduce a body inside her body(nested).

# Reset l1 = ['a', 'b', 'c']
l1.extend(l2)
l1
['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'},
                'Remove': {'pop', 'remove', 'clear'}
                'Sort': {'reverse', 'sort'},
                'Search': {'count', 'index'},
                'Copy': {'copy'},
                }
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
3

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

>>> a
[1, 2, 3]
a.extend([4])  #is eqivalent of a[len(a):] = [4]
>>> a
[1, 2, 3, 4]
a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a[len(a):] = [4]
>>> a
[1, 2, 3, 4]
Georgy
  • 12,464
  • 7
  • 65
  • 73
tessie
  • 964
  • 3
  • 14
  • 24
0

append "extends" the list (in place) by only one item, the single object passed (as argument).

extend "extends" the list (in place) by as many items as the object passed (as argument) contains.

This may be slightly confusing for str objects.

  1. If you pass a string as argument: append will add a single string item at the end but extend will add as many "single" 'str' items as the length of that string.
  2. If you pass a list of strings as argument: append will still add a single 'list' item at the end and extend will add as many 'list' items as the length of the passed list.
def append_o(a_list, element):
    a_list.append(element)
    print('append:', end = ' ')
    for item in a_list:
        print(item, end = ',')
    print()

def extend_o(a_list, element):
    a_list.extend(element)
    print('extend:', end = ' ')
    for item in a_list:
        print(item, end = ',')
    print()
append_o(['ab'],'cd')

extend_o(['ab'],'cd')
append_o(['ab'],['cd', 'ef'])
extend_o(['ab'],['cd', 'ef'])
append_o(['ab'],['cd'])
extend_o(['ab'],['cd'])

produces:

append: ab,cd,
extend: ab,c,d,
append: ab,['cd', 'ef'],
extend: ab,cd,ef,
append: ab,['cd'],
extend: ab,cd,
ilias iliadis
  • 601
  • 8
  • 15
0

Append and extend are one of the extensibility mechanisms in python.

Append: Adds an element to the end of the list.

my_list = [1,2,3,4]

To add a new element to the list, we can use append method in the following way.

my_list.append(5)

The default location that the new element will be added is always in the (length+1) position.

Insert: The insert method was used to overcome the limitations of append. With insert, we can explicitly define the exact position we want our new element to be inserted at.

Method descriptor of insert(index, object). It takes two arguments, first being the index we want to insert our element and second the element itself.

Example: my_list = [1,2,3,4]
my_list[4, 'a']
my_list
[1,2,3,4,'a']

Extend: This is very useful when we want to join two or more lists into a single list. Without extend, if we want to join two lists, the resulting object will contain a list of lists.

a = [1,2]
b = [3]
a.append(b)
print (a)
[1,2,[3]]

If we try to access the element at pos 2, we get a list ([3]), instead of the element. To join two lists, we'll have to use append.

a = [1,2]
b = [3]
a.extend(b)
print (a)
[1,2,3]

To join multiple lists

a = [1]
b = [2]
c = [3]
a.extend(b+c)
print (a)
[1,2,3]
vivek
  • 563
  • 7
  • 16