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)

The extend method helps us to add all the items in New_List at the end of the Old_list.

Python extend List Function Example

The extend function adds the specified list to the end of a given list. The below code add Lists b to the end of List a

# Extend List Items in python

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

print("Original List Items are : ", a)
a.extend(b)
print("List Items are          : ", a)

List extend Function output

Original List Items are :  [10, 20, 30, 40]
List Items are          :  [10, 20, 30, 40, 70, 120]

List extend Example 2

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

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

# Python Extend function on List Items

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 List extend function on the Mixed List. I mean, adding a string list to integer list

# Python Extend List Items

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

print("Original List Items are : ", a)
a.extend(b)
print("List Items are          : ", a)

List extend Function output

Original List Items are :  [10, 20, 30, 40]
List Items are          :  [10, 20, 30, 40, 'Apple', 'Kiwi']

List extend Function Example 4

In this program, we used extend function on the Nested list (list inside a list).

# Python Extend function on List Items

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

print("Original List Items are : ", Nested)
Nested.extend(NewOne)
print("List Items are          : ", Nested)

List extend Function output

Original List Items are :  [[71, 222], [22, 13], [99, 77]]
List Items are          :  [[71, 222], [22, 13], [99, 77], [11, 22], [44, 55]]