Python List extend() Function
The List extend()
method add the elements of an iterable (such as
a list, tuple, string,
or set) to the end of the current list.
Syntax
my_list.extend(iterable)
extend() Parameters
Python List extend()
method parameters::
Parameter | Condition | Description |
---|---|---|
iterable | Required | Any iterable (list, tuple, string, or set, etc.) |
extend() Return Value
Python List extend()
function does not return anything: it modifies the original list.
Examples
Example 1: Extending a List with Another List
For example, let's extend a list with another list:
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
output
[1, 2, 3, 4, 5, 6]
Example 2: Extending a List with a Set
For example, let's extend a list with a set:
my_list = ['a', 'b', 'c']
my_set = {'d', 'e', 'f'}
my_list.extend(my_set)
print(my_list) # Output: ['a', 'b', 'c', 'd', 'e', 'f']
output
['a', 'b', 'c', 'd', 'e', 'f']
Example 3: Extending a List with a String
For example, let's extend a list with characters of a string:
my_list = ['Tom', 'Ryan', 'David']
my_list.extend('Anna')
print(my_list) # Output: ['Tom', 'Ryan', 'David', 'A', 'n', 'n', 'a']
output
['Tom', 'Ryan', 'David', 'A', 'n', 'n', 'a']
Example 4: Extending a List with a Single Element
Of course, you can add a single element to a list: just consider this element as an iterable of a single element (like a list).
my_list = [1, 2]
my_list.extend([3])
print(x) # Output: [1, 2, 3]
output
[1, 2, 3]
extend() method vs append() method
The extend()
method treats its argument as an iterable object.
For example, when you pass a string (iterable object) as an argument, the method adds every character to a list instead of the string.
my_list = ['Tom', 'Ryan', 'David']
my_list.extend('Anna')
print(my_list) # Output: ['Tom', 'Ryan', 'David', 'A', 'n', 'n', 'a']
output
['Tom', 'Ryan', 'David', 'A', 'n', 'n', 'a']
Instead, use append()
method:
my_list = ['Tom', 'Ryan', 'David']
my_list.append('Anna')
print(my_list) # Output: ['Tom', 'Ryan', 'David', 'Anna']
output
['Tom', 'Ryan', 'David', 'Anna']
The key difference is:
append()
adds its argument as a single element to the end of a list.extend()
iterates over its argument, adding each element to the list, extending the list by however many elements were in the iterable argument
Equivalent Methods to extend() method
Alternative 1: Zero-length slice
Specifying a zero-length slice at the end is also equivalent to extend()
method.
my_list = ['Tom', 'David', 'Anna']
my_list[len(my_list):] = [1,2,3]
print(my_list) # Output: ['Tom', 'David', 'Anna', 1, 2, 3]
output
['Tom', 'David', 'Anna', 1, 2, 3]
Alternative 2: Concatenation +
operator
Using the concatenation operator +
or the augmented assignment operator +=
on a list is equivalent to
using extend()
.
An example using the concatenation operator +
on a list:
my_list = ['Tom', 'David', 'Anna']
my_list = my_list + [1,2,3]
print(my_list) # Output: ['Tom', 'David', 'Anna', 1, 2, 3]
output
['Tom', 'David', 'Anna', 1, 2, 3]
An example using the augmented assignment operator +=
on a list:
my_list = ['Tom', 'David', 'Anna']
my_list += [1,2,3]
print(my_list) # Output: ['Tom', 'David', 'Anna', 1, 2, 3]
output
['Tom', 'David', 'Anna', 1, 2, 3]