The Python list extend() function is useful for adding all items from an iterable to the end of the existing list. It accepts an iterable, such as a string, list, tuple, set, or dictionary, as a parameter and merges it with the existing list. In short, the extend() function adds/appends multiple items in an iterable to an existing list.
Python list extend() syntax
The syntax of the list extend() function to add multiple items to an existing list is
listName.extend(iterable)
From the above syntax, the listName is the original list to which we want to add new items.
The iterable can be any object, such as a list, string, tuple, set, etc. The Python list extend() function adds every individual item in this iterable as a separate entry to the listName.
Return Value: The extend() function modifies the original list by adding the items from a given iterable object and returns None. In short, the extend() method won’t return any value as output.
Python list extend() function Example
As we all know, the extend() function extends the existing list by appending the items in a given iterable to the end. To demonstrate this, we use the following example to merge or combine two lists.
In the example below, we declared two lists with integer items, and our task is to merge them. Here, a.extend(b) statement extracts or unpacks all items in the “b” list and appends them to the “a” list. It means the Python list extend() function adds both 70 and 120 in “b” to the end of list “a” (after 40). The final list becomes [10, 20, 30, 40, 70, 120].
Here, the length of the “a” list changed from 4 to 6 because of the extend() function. However, list “b” is unchanged, and it still contains 70 and 120.
a = [10, 20, 30, 40]
b = [70, 120]
print("Original Items are : ", a)
a.extend(b)
print("New Items are : ", a)
Original Items are : [10, 20, 30, 40]
New Items are : [10, 20, 30, 40, 70, 120]
Combine Multiple lists
As we all know, the Python list extend() function accepts one argument, so we must call the extend() multiple times to combine multiple lists. For example, in the example below, we declared three integer lists.
- The first extend() statement merges list a and b. It means both 40 and 50 are added to the end of the list “a”. Result = [10, 20, 30, 40, 50]
- The next statement combines a (already merged with b) and c. It means 60, 70, 80, and 90 values are added to the end of a (after 50).
a = [10, 20, 30]
b = [40, 50]
c = [60, 70, 80, 90]
a.extend(b)
print(a)
a.extend(c)
print(a)
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50, 60, 70, 80, 90]
How to use the Python extend() function on the mixed list?
As we all know, a list may contain a different kind of data (data types), and the extend() function seamlessly works on mixed data type lists.
Let me use the extend() function on the Mixed elements by adding a list of string words to an integer list.
a = [10, 20, 30, 40]
b = ['Apple', 'Kiwi']
a.extend(b)
print(a)
[10, 20, 30, 40, 'Apple', 'Kiwi']
Adding integer
When you use an integer as the Python list extend() function argument, the function will raise a TypeError. As we all know, the extend() method only accepts iterables, and an integer is not an iterable.
x = ['apple', 'banana']
y = 100
x.extend(y)
print(x)
TypeError: 'int' object is not iterable
TIP: Use the append() function on an integer. Please replace the x.extend(y) line with x.append(y), and 100 becomes the last list item.
Using Python extend() function on Nested Lists
Similar to the regular lists, the extend() function helps combine two nested lists. In this program, we used the extend() function on two nested lists and merged them into a single.
Nested = [[71, 222], [22, 13], [99, 77]]
New = [[11, 22], [44, 55]]
Nested.extend(New)
print(Nested)
[[71, 222], [22, 13], [99, 77], [11, 22], [44, 55]]
Apart from this, we can utilize the extend() function to convert a nested list (list of lists) into a normal list. In the example below, the for loop iterate the list items, where each sub-list acts as a single item.
On each iteration, the Python list extend() function appends all items in a sub-list to “b”, and the process stops when it reaches the end position.
a = [[71, 222], [22, 13], [99, 77]]
b = []
for n in a:
b.extend(n)
print(b)
[71, 222, 22, 13, 99, 77]
Python list extend() function on strings
In this example, we declared two lists consists of string elements. Next, we used this list extend() function to merge those string lists.
Please refer to the List article and List functions article to understand everything about them in the Python Programming language.
Fruits = ['Apple', 'Orange', 'Kiwi']
strFruit = ['Banana', 'Cherry']
print("Original Items are : ", Fruits)
Fruits.extend(strFruit)
print("Items After are : ", Fruits)

It’s not about combining two lists of string words. We can use the extend() function to add individual characters in a string as the list items.
For instance, in the example below, we declared a list of individual characters and a string. When we use the Python list extend() function to append the string to this character list, it won’t add “hello” at the end. Instead, it unpacks all characters in “hello” (h, e, l, l, o) and treats them as individual entries. So, it adds those items at the end.
x = ['a', 'b', 'c']
y = "hello"
x.extend(y)
print(x)
['a', 'b', 'c', 'h', 'e', 'l', 'l', 'o']
Some users may think that the extend() function changed the string to characters because the old list has the characters. No, the extend() function always treats the string as a group of characters and reads each character as a separate item.
For example, here, the main list contains two string words, and when we use extend to join the “hello” word, it unpacks and adds individual characters.
x = ['apple', 'banana']
y = "hello"
x.extend(y)
print(x)
['apple', 'banana', 'h', 'e', 'l', 'l', 'o']
NOTE: Whether the argument (y) is a digit or an alphabet, if it is enclosed in quotes, it is considered as a string and an individual digit or character from that string is added to a list as a separate list item.
Python list extend() function with a tuple
Similar to the above list-based example, we can use the extend() function to add the tuple items to a list.
The following example shows how to combine list and tuple items using the extend() function. Here, we declared a list of three integer items and a tuple with 4 floating-point numbers. The extend() function adds or appends all items in a tuple (b) to the end of the list (a).
a = [11, 22, 33]
b = (15.5, 25.9, 35.11, 45.4)
a.extend(b)
print(a)
[11, 22, 33, 15.5, 25.9, 35.11, 45.4]
Python list extend() function with set
Similar to lists and tuples, we can use the extend() function with a set iterable parameter to merge lists and sets.
In the example below, we declared a list and a set of four items each. Next, we utilized the set as the extend () function argument to combine a list and a set. As the set does not follow the order of set items, the insertion order may differ from the original order.
a = [11, 12, 13, 14]
b = {5, 10, 15, 20}
a.extend(b)
print(a)
If you observe the last four elements coming from the set, the order is different from the original set. The extend() function won’t change the set.
[11, 12, 13, 14, 10, 20, 5, 15]
Python list extend() with a dictionary
Similar to the other itearbles, we can use the list extend() function on dictionary items. However, by default, the extend() function appends the dictionary keys to the end of the list. It won’t read the dictionary value to append.
a = [11, 12, 13]
b = {'a':2, 'b':4, 'c':6, 'd':8}
a.extend(b)
print(a)
[11, 12, 13, 'a', 'b', 'c', 'd']
To append or add the dictionary values to the list (instead of keys), we must explicitly call the values() function to read values.
In the example below, we replaced “b” with “b.values()”. Here, the values() function helps to read values, and the extend function reads those values and adds them to a list.
a = [11, 12, 13]
b = {'a':2, 'b':4, 'c':6, 'd':8}
a.extend(b.values())
print(a)
[11, 12, 13, 2, 4, 6, 8]
Alternatives to Python extend() function
Apart from the list extend() function, we can use the + or += operators to combine more than two lists or iterables. Otherwise, use the slicing option or the chain() method. This section provides an example of them.
Using the Arithmetic + operator
The arithmetic + operator is used to perform concatenation, which can be string concatenation or merging lists. By default, the + operator creates a new list object to store the result of merging two lists. On the other hand, the extend() function modifies the original list. So, the performance of the Python list extend() function is higher for large data.
a = [11, 22, 33, 44]
b = [55, 66, 77]
c = a + b
print(c)
[11, 22, 33, 44, 55, 66, 77]
However, we can utilize the += assignment operator to avoid creating a new list object. The following code modifies the original instead of creating a new list.
a = [11, 22, 33, 44]
b = [55, 66, 77]
a += b
print(a)
[11, 22, 33, 44, 55, 66, 77]
TIP: With the help of the + or += operator, we can combine or merge more than two lists in a single statement. On the other hand, the extend() function allows only one item as input and merges only two lists at a time.
Using the slicing technique
As we all know, the Python list extend() function appends all items from the given iterable to the end of the existing list. By using the slicing technique, we can achieve it.
In the example below, we declared two integer lists. Next, we utilized the built-in len() function to find the total number of items in a list. It helps to find the list endpoint so that we can start appending new values from the second list.
As we know, the slicing technique accepts optional start and end index positions. We set the starting position as the last item plus one position. Next, keep the end position empty for the dynamic nature and assign it to list b. It means the total number of list items in b is appended to the end of a list. Same as the Python list extend() function.
a = [22, 33, 55]
b = [66, 77]
a[len(a):] = b
print(a)
[22, 33, 55, 66, 77]
Using itertools chain() method
We can import the chain() method from itertools and utilize it to combine two ietarbles. However, the chain() function creates a new chain object, requiring extra memory. Next, we must utilize the list() function to convert a chain object to a list.
from itertools import chain
a = [22, 33, 55]
b = [66, 77]
c = chain(a, b)
print(list(c))
[22, 33, 55, 66, 77]
Difference between Python list extend() and append() functions
Both the list extend() and append() functions add new items to the existing list and do not return a value as output. They modify the original list by adding the new items. However, the append() function adds a single item of any data type to an existing list. On the other hand, the extend() function accepts iterables and appends multiple items to a list.
myList.append(item)
myList.extend(iterable)
To add a single item, such as an integer, floating-point number, or string, use the append() function. To add multiple items, such as combining two lists, a list and a tuple, a list and a set, a list and a dictionary, use the extend function.
The append() function in the example below adds 90 to the end of an existing list. The Python list extend() function combines lists a and b by adding all list items in b to the end of a.
a = [10, 20, 30, 40]
b = [100, 200]
a.append(90)
print(a)
a.extend(b)
print(a)
[10, 20, 30, 40, 90]
[10, 20, 30, 40, 90, 100, 200]