Python List extend()

The extend() method in Python adds the iterable elements (list, tuple, string etc.) to the end of the list. The length of the list is increased by the number of elements present in the iterable.

In this tutorial, we will learn about the Python list extend() method with the help of examples.

Syntax of List extend() 

The syntax of the extend() method is:

list.append(iterable)

extend() Parameters

The extend() method takes a single parameter.

  • iterable – any iterable such as list, set, string tuple etc.

Return Value from List extend()

The extend() method modifies the list by adding the iterable elements to the end of the list, but it does not return any value.

Difference between append() and extend() method in Python

The append() method adds its argument as a single element to the end of the list and the length of the list will be increased by one. Whereas the extend() method iterates over the argument adding each items to the end of the list and the list is increased by the number of items added through iteration.

a =[1,2]
b= [3,4]

# append() method 
a.append(b)
print("Using append() method", a)

x =[1,2]
y= [3,4]


# extend() method 
x.extend(y)
print("Using extend() method", x)

Output

Using append() method [1, 2, [3, 4]]
Using extend() method [1, 2, 3, 4]

Example 1: How to use List extend() Method

# Programming list
programming_list = ['C','C#','Python','Java']

frontend_programming =['CSS','HTML','JavaScript']

# add the frontend_progamming list into the existing list
programming_list.extend(frontend_programming)

# Note that iterable element is added to the end of the list
print('The new extended list is :', programming_list)

Output

The new extended list is : ['C', 'C#', 'Python', 'Java', 'CSS', 'HTML', 'JavaScript']

Example 2: Add Elements of Tuple and Set to List

# Programming list
programming_list = ['C','C#','Python','Java']

# frontend tuple
frontend_programming =('CSS','HTML','JavaScript')

# DB set
db_set ={'SQL','NoSQL'}

# add the tuple to the existing list
programming_list.extend(frontend_programming)

# print the extended list after the adding tuple
print('The new extended list after adding tuple is :', programming_list)

# add the set to the existing list
programming_list.extend(db_set)

# print the extended list after the adding set
print('The new extended list after adding set is :', programming_list)

Output

The new extended list after adding tuple is : ['C', 'C#', 'Python', 'Java', 'CSS', 'HTML', 'JavaScript']
The new extended list after adding set is : ['C', 'C#', 'Python', 'Java', 'CSS', 'HTML', 'JavaScript', 'SQL', 'NoSQL']

Example 3: Extend String to the List

Here the sting is also an iterable element. Hence we can extend the string to the list and each character in the string is appended into the list.

#list of characters
my_list =['z','x','c']

# string with vowels
vowels ='aeiou'

# extend sting to list
my_list.extend(vowels)
print(my_list)

Output

['z', 'x', 'c', 'a', 'e', 'i', 'o', 'u']
Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like