extend() method in a python list adds all the elements of an iterable to the end of the list. Iterable here means a structure that can be iterated or loop over such as a list, a tuple, a set or a string.
extend() accepts a single argument and returns None.

extend() syntax
Syntax of extend() method is

list.extend(iterable)

extend() iterates over the supplied iterable and adds each of its element to the list.
Here, iterable may be a list, tuple or a string.

If the iterable is a list or tuple, then extend() will add each of its element to the list.
If it is a string, then extend() adds each character of the string to the list.
Remember that extend() modifies the original list, it does not create a new one.
Example
Below is an example of extend() method with list argument.

# create a list
l = [1, 2, 3]
# create another list
l1 = [4, 5, 6]
print('Before extend:', l)
l.extend(l1)
print('After extend:', l)

This prints

Before extend: [1, 2, 3]

After extend: [1, 2, 3, 4, 5, 6]

Elements of second list or argument list supplied to extend() are added after the first list.

extend() with tuple and set
extend() example with tuple and set as arguments is given below.

# create a list
l = [ 1, 2, 3 ]
# create a set
s = { 4, 5, 6 }
print('Before extend:', l)
l.extend(s)
print('After extend with set:', l)
# create a tuple
t = { 1.2, 3.4 }
l.extend(t)
print('After extend with tuple:', l)
Before extend: [1, 2, 3]
After extend with set: [1, 2, 3, 4, 5, 6]

After extend with tuple: [1, 2, 3, 4, 5, 6, 1.2, 3.4]

Notice that extend() modifies the original list.
extend() with string argument
With a string argument, extend() adds each character of the string as a list element. Example,

# create a list
l = [ 1, 2, 3 ]
print('Before extend:', l)
l.extend('codippa')
print('After extend:', l)
Before extend: [1, 2, 3]

After extend: [1, 2, 3, ‘c’, ‘o’, ‘d’, ‘i’, ‘p’, ‘p’, ‘a’]

Look how the characters of the string are added to the list.

Difference between append and extend
Both append() and extend() are used to add elements to a list but there are a few differences between the two methods.
1. append() accepts a value as argument while extend() accepts an iterable.
2. append() adds the argument value as it is while extend() will add individual elements of the iterable to the list.

append() vs extend() example with a list argument is shown below.

# create a list
l = [ 1, 2, 3 ]
print('Before append:', l)
# create another list
l1 = [ 4, 5, 6 ]
l.append(l1)
print('After append:', l)

# initialize list again
l = [ 1, 2, 3]
print('Before extend:', l)
l.extend(l1)
print('After extend:', l)
Before append: [1, 2, 3]
After append: [1, 2, 3, [4, 5, 6]]
Before extend: [1, 2, 3]

After extend: [1, 2, 3, 4, 5, 6]

extend() vs +
Elements of two or more lists can also be combined using a +(plus operator) as extend() but there are some differences between the two.
1. + is an operator while extend() is a method.
2. + does not modify the original list, it creates a new list while extend() modifies the original list, it is a mutable operation.
If you merge multiple lists using a +, you need to assign the result to a list to get the desired result.
Example,

l = [ 1, 2, 3 ]
l1 = [4, 5, 6]
l + l1
print('First list after adding:', l)
final_list = l + l1
print('New list after adding:', final_list)

Output is

First list after adding: [1, 2, 3]

New list after adding: [1, 2, 3, 4, 5, 6]

Look, when the result was not assigned, the first list remains the same as opposed to extend() which modifies the original list.
When the result of + is assigned to a new variable, it contains the merged elements.

Extend multiple lists
For extending multiple list with extend() method, you need to call it multiple times since there is no provision to supply multiple arguments to extend() at the same time.
Example,

# create a list
l = [ 1, 2, 3 ]
print('Before extend:', l)
# create another list
l1 = [ 4, 5, 6 ]
# create a third list
l2 = [ 7, 8, 9 ]
# call extend twice
l.extend(l1)
l.extend(l2)
print('Final list:', l)

You can now guess the output.

Before extend: [1, 2, 3]

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

Hit the clap button below if you liked the post.

Leave a Reply