Python extend List Function

Python extend List is one of the List functions used to add a new list to the end of an old list. In this section, we discuss how to use this Python List extend function with practical examples. The syntax of this extend list function is:

Old_List.extend(New_List)

This method helps us to add all the items in NewList at the end of the OldList.

Python extend List Function Example

The extend list method append and extend given elements to the list. The below code add Lists b to the end of a

a = [10, 20, 30, 40]
b = [70, 120]

print("Original Items are : ", a)
a.extend(b)
print("Items are          : ", a)
Original Items are :  [10, 20, 30, 40]
Items are          :  [10, 20, 30, 40, 70, 120]

In this example, we declared two string list. Next, we used the extend function on them.

TIP: Please refer to List article and functions article to understand everything about them in Python programming language.

Fruits = ['Apple', 'Orange', 'Kiwi']
strFruit = ['Banana', 'Cherry']

print("Original List Items are : ", Fruits)
Fruits.extend(strFruit)
print("List Items are          : ", Fruits)
Python Extend List Function 3

Let me use this extend function on the Mixed List. I mean, adding a string to integer.

a = [10, 20, 30, 40]
b = ['Apple', 'Kiwi']

print("Original Items are : ", a)
a.extend(b)
print("Items are          : ", a)
Original Items are :  [10, 20, 30, 40]
Items are          :  [10, 20, 30, 40, 'Apple', 'Kiwi']

In this program, we used extend function on the Nested list.

Nested = [[71, 222], [22, 13], [99, 77]]
NewOne = [[11, 22], [44, 55]]

print("Original Elements are : ", Nested)
Nested.extend(NewOne)
print("Elements are          : ", Nested)
Original Elements are :  [[71, 222], [22, 13], [99, 77]]
Elements are          :  [[71, 222], [22, 13], [99, 77], [11, 22], [44, 55]]