Methods and Functions

Python List extend()

What if we want to add more values to our iterable objects? Python provides extend as a feature to carry out this operation. It is both a keyword and a method. Python extend() is a built-in method that can be used to extend the entries of a list or array. When the given list has to be expanded, we utilize the extend() method. This method appends all of the elements to the original list. This article focuses on the Python extend() function for lists.

Python extend

Definition

  • The Python extend() list function is used to add elements of an iterable (such as string, list, tuple, set, etc.) to the end of another list.
  • Python list extend() method is used for extending a list by adding all the elements of the iterable at the end of the previous list.

Syntax of List extend()

The list extend() function follows the below-mentioned syntax:

                    

list_name.extend(iterable)

Here, all the elements of the iterable are added to the end of list_name.

extend() Parameters

The extend() function accepts parameter:

  • iterable – a collection of elements that can either be a list, tuple, set, string, etc.

Return value from extend()

The Python extend() function modifies the original list and returns the same list with new elements added to the end of the list.

Example 1: Using extend() Method

Let’s look at a simple example of the extend() method being used on a list.

Example

                    

# Python program to illustrate list extend()
# original list
devices = ['Mobile', 'Laptop', 'TV', 'HardDisk']
# another list
other_dev = ['Printer', 'Scanner']

# extending original list with another list
devices.extend(other_dev)
print('Devices List:', devices)

Output

                    

Devices List: ['Mobile', 'Laptop', 'TV', 'HardDisk', 'Printer', 'Scanner']

Example 2: Add Elements of Tuple, Set, and String to List

Note – Because a string is an iterable, if you use it to extend a list, you will append each character as you iterate over the string.

Example

                    

# Python program to illustrate list extend()
# original list
proglang = ['C', 'C++']

# tuple
lang2 = ['Python', 'Java']

# set
lang3 = {'Ruby', 'R', 'PHP'}

# string
lang4 = 'MATLAB'

# extending original list with lang2 tuple
proglang.extend(lang2)
print('Programming Languages List:', proglang)
print('')

# extending original list with lang3 set
proglang.extend(lang3)
print('Programming Languages List:', proglang)
print('')

# extending original list with lang4 string
proglang.extend(lang4)
print('Programming Languages List:', proglang)

Output

                    

Programming Languages List: ['C', 'C++', 'Python', 'Java']

Programming Languages List: ['C', 'C++', 'Python', 'Java', 'Ruby', 'R', 'PHP']

Programming Languages List: ['C', 'C++', 'Python', 'Java', 'Ruby', 'R', 'PHP', 'M', 'A', 'T', 'L', 'A', 'B']

Other Methods to Extend a List

We can also extend the list by using the following different methods:

  1. The + operator –

We can add the elements of another iterable to our original list by simply using the ‘+’ operator between the 2 objects. It can, however, only be used with the same type of iterables, such as adding list to list or tuple to a tuple.

Example

                    

list1 = [1, 2, 3]
list2 = [7, 8, 9]

list1 = list1 + list2
print('list1 = ', list1)

Output

                    

list1 =  [1, 2, 3, 7, 8, 9]

  1. List slicing syntax

Example

                    

list1 = [1, 2, 3]
list2 = [7, 8, 9]

list1[len(list1):] = list2
print('list1 = ', list1)

Output

                    

list1 =  [1, 2, 3, 7, 8, 9]

  1. Using chain() –

We can extend a list using the chain() iterator method. To use this method we need to import the itertools module first. The syntax is as follows:

                    

list(chain(main_list, [values]))

Example

                    

from itertools import *

num = [5, 10, 15]
z = list(chain(num, [20, 25, 30]))
print(z)

Output

                    

[5, 10, 15, 20, 25, 30]

Frequently Asked Questions

Q1. What is extend() in Python?

The Python extend() list function is used to add elements of an iterable (such as string, list, tuple, set, etc.) to the end of another list. When the given list has to be expanded, we utilize the extend() method. This method appends all of the elements to the original list.

Q2. How do you expand a list in Python?

To expand a list in Python, we make use of the Python list extend() function. The list extend() function follows the below-mentioned syntax:

                    

list_name.extend(iterable)

Example

                    

color1 = ['Red', 'Blue', 'Green']
color2 = ['Black', 'White', 'Pink']

color1.extend(color2)
print('List of colors:', color1)

Output

                    

List of colors: ['Red', 'Blue', 'Green', 'Black', 'White', 'Pink']

Q3. How do you expand 2 lists in Python?

To expand 2 lists in Python, we have different methods. A few of them are:

  1. Using + operator
  2. Using chain() function
  3. By Using * operator

Example

                    

from itertools import *

games = ['Cricket', 'Hockey', 'Football']
newgames = ['Baseball', 'Swimming']
print('List of games:', games+newgames)

num = [1, 2, 3, 4]
print('List of numbers:', list(chain(num, [5, 6, 7])))

name = ['Sam', 'Kim']
age = [23, 24]
x = [*name, *age]
print('List:', x)

Output

                    

List of games: ['Cricket', 'Hockey', 'Football', 'Baseball', 'Swimming']
List of numbers: [1, 2, 3, 4, 5, 6, 7]
List: ['Sam', 'Kim', 23, 24]

Q4. What is the difference between extend() and append()?

The extend() method adds items from the provided iterable to the list’s end, whereas the append() method adds the entire iterable as an item to the list’s end. In addition, when you supply a string (iterable object) as a parameter, the method adds every character to a list rather than the string. The append() function, on the other hand, adds the entire string as a single value.

Example

                    

city = ['Mumbai', 'London']
favcity = ['New York', 'Toronto']
mycity = {'Tokyo'}

city.extend(favcity)
print('city List after extend:', city)
favcity.append(mycity)
print('favcity List after append:', favcity)
print('')

col = ['Black', 'Blue']
col2 = ['Red']
col.extend(col2)
print(col)

col3 = ['Black', 'Blue']
col4 = ['Red']
col3.append(col4)
print(col3)

Output

                    

city List after extend: ['Mumbai', 'London', 'New York', 'Toronto']
favcity List after append: ['New York', 'Toronto', {'Tokyo'}]

['Black', 'Blue', 'Red']
['Black', 'Blue', ['Red']]

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.