Python List extend() Method with Examples

In the previous article, we have discussed Python List count() method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List extend() Method in Python:

The extend() method appends all iterable (list, tuple, string, etc.) items to the end of the list.

Syntax:

gvnlist.extend(iterable)

All iterable elements are appended to the end of gvnlist in this case.

Parameter Values: 

As previously stated, the extend() method accepts an iterable such as a list, tuple, string, and so on.

Return Value:

The extend() method makes changes to the original list. It doesn’t return anything.

Examples:

Example1:

Input:

Given Main list = ['hello', 'this']
Given Second List = ['python', 'programs']

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs']

Example2:

Input:

Given Main list = ['hello', 'this']
Given Second Tuple= ('python', 'programs', 'python')

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs', 'python']

List extend() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example-1: Adding Another List

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the list which we want to extend as static input and store it in another variable.
  • Extend the second list to the main list using the extend() function(Here it appends the second list to main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the list which we want to extend as static input and store it in another variable.
scndlist = ['python', 'programs']
# Extend the second list to the main list using the extend()
# function(Here it appends the second list to main list)

mainlst.extend(scndlist)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs']
Example-2: Adding Another Set

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the set which we want to extend as static input and store it in another variable.
  • Extend the second set to the main list using the extend() function(Here it appends the second set to main list) and store it in a variable resltlst.
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the set which we want to extend as static input and store it in another variable.
scndset = {'python', 'programs', 'python'}
# Extend the second set to the main list using the extend()
# function(Here it appends the second set to main list)

mainlst.extend(scndset)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'programs', 'python']
Example-3: Adding Another Tuple

Approach:

  • Give the main list as static input and store it in a variable.
  • Give the tuple which we want to extend as static input and store it in another variable.
  • Extend the second tuple to the main list using the extend() function(Here it appends the second tuple to the main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as static input and store it in a variable.
mainlst = ['hello', 'this']
# Give the tuple which we want to extend as static input and store it in another variable.
scndtup = ('python', 'programs', 'python')
# Extend the second tuple to the main list using the extend() function
# (Here it appends the second tuple to the main list).

mainlst.extend(scndtup)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

The Modified list after extending is :  ['hello', 'this', 'python', 'programs', 'python']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the main list as user input using list(),int(),split() and map() functions and store it in a variable.
  • Give the list which we want to extend as user input using list(),int(),split() and map() functions and store it in another variable.
  • Extend the second list to the main list using the extend() function(Here it appends the second list to main list).
  • Print the elements of the main list.
  • The Exit of the Program.

Below is the implementation:

# Give the main list as user input using list(),int(),split() and map() functions and store it in a variable.
mainlst = list(
    map(int, input('Enter some random elements of the main list = ').split()))
# Give the list which we want to extend as user input using list(),int(),split() and map() functions
# and store it in another variable.
scndlst =  list(
    map(int, input('Enter some random elements of the second list = ').split()))
# Extend the second list to the main list using the extend() function
# (Here it appends the second list to the main list).

mainlst.extend(scndlst)
# Print the mainlist
print('The Modified list after extending is : ', mainlst)

Output:

Enter some random elements of the main list = 0 3 0 8
Enter some random elements of the second list = 2 8 3 7 9 4
The Modified list after extending is : [0, 3, 0, 8, 2, 8, 3, 7, 9, 4]

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.