The Python extend() method adds all items from an
iterable to the end of an existing
list.
Unlike append(), which adds its argument as one item, extend() reads the iterable and adds its elements individually.
my_list.extend(iterable)
The extend() method accepts one argument. The argument must be an iterable such as a list, tuple, string, set, dictionary, or range object.
Each item from the iterable is added to the end of the existing list.
A common use of extend() is to add the elements of one list to another list.
my_list1=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']
my_list1.extend(my_list2)
print(my_list1)
Output
['Alex', 'Ronald', 'John', 'Ravi', 'Alen']
The individual items from my_list2 are added separately to my_list1.
The main difference is how the supplied object is added.
append() adds the complete object as one new list item.extend() adds each element of the iterable separately.my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']
my_list.append(my_list2)
print(my_list)
Output
['Alex', 'Ronald', 'John', ['Ravi', 'Alen']]
The complete second list becomes one item inside my_list.
my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']
my_list.extend(my_list2)
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi', 'Alen']
Here, 'Ravi' and 'Alen' are added as separate list items.
A string is iterable, so extend() reads the string one character at a time.
my_list=['a', 'b', 'c', 'd']
my_string='xyz'
my_list.extend(my_string)
print(my_list)
Output
['a', 'b', 'c', 'd', 'x', 'y', 'z']
The string 'xyz' is not added as one string. Its three characters are added separately.
If 'xyz' should be added as one item, use
append() instead.
A tuple is also iterable, so its elements can be added to a list using extend().
my_list=[1, 2, 3]
my_tuple=(4, 5)
my_list.extend(my_tuple)
print(my_list)
Output
[1, 2, 3, 4, 5]
The range() function returns an iterable sequence of integers. These values can be added directly to a list using extend().
my_list=[1, 2]
my_list.extend(range(3, 6))
print(my_list)
Output
[1, 2, 3, 4, 5]
The values generated by range(3,6) are 3, 4, and 5.
A set is iterable, so its elements can also be added to a list.
my_list=[1, 2, 3]
my_set={4, 5}
my_list.extend(my_set)
print(my_list)
Sample Output
[1, 2, 3, 4, 5]
A set does not guarantee a fixed element order, so the order in which 4 and 5 are added should not be relied upon.
A dictionary is iterable too. When a dictionary is passed directly to extend(), its keys are added to the list.
my_list=['Alex']
my_dict={
'name': 'Ravi',
'city': 'Delhi'
}
my_list.extend(my_dict)
print(my_list)
Output
['Alex', 'name', 'city']
The dictionary values 'Ravi' and 'Delhi' are not added because iterating over a dictionary directly produces its keys.
To add the dictionary values instead, pass my_dict.values() to extend().
my_list=['Alex']
my_dict={
'name': 'Ravi',
'city': 'Delhi'
}
my_list.extend(my_dict.values())
print(my_list)
Output
['Alex', 'Ravi', 'Delhi']
An empty list can be filled with the items of another iterable.
my_list=[]
my_list.extend([1, 2, 3])
print(my_list)
Output
[1, 2, 3]
All three elements from the supplied list are added to the empty list.
The extend() method modifies the existing list in place and returns None.
my_list=[1, 2]
result=my_list.extend([3, 4])
print(result)
print(my_list)
Output
None
[1, 2, 3, 4]
Since extend() returns None, assigning its result back to the same variable replaces the list variable with None.
my_list=[1, 2]
my_list=my_list.extend([3, 4])
print(my_list)
Output
None
Call extend() directly when you want to add the iterable's elements to the existing list.
The argument supplied to extend() must be iterable. A single integer is not iterable.
my_list=[1, 2]
my_list.extend(3)
Sample Error
TypeError: 'int' object is not iterable
To add the integer 3 as one item, use
append(3).
my_list=[1, 2]
my_list.append(3)
print(my_list)
Output
[1, 2, 3]
extend() adds all items from an iterable to an existing list.list.extend(iterable).extend() is None.append() when the supplied object should be added as one item.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.