Python list extend() method [Practical Examples]


Written by - Deepak Prasad

Python List extend() method

In Python, the list is an ordered and mutable container. The extend() method is used to add the items from other iterable to the existing list. In other words, extend() method extends the list by appending all the items from the iterable. So, for an existing list "a", it is equivalent to the statement a[len(a):] = iterable.

 

Syntax of list.extend() method

The statement below shows the syntax of the extend() method. Here, the iterable can be a list, a tuple, a set, a string or, a dict. However, this method does not return any value.

list.extend(iterable)

 

Examples demonstrating extend() method of list

Example 1 : Merge two list using extend() method

In this example, we are having two lists containing city names. Here, we will append the content of list l2 with the list l1.

# Initializing list
l1=["Delhi","Bhopal","Hyderabad","Bengaluru"]
l2=["Mumbai","Pune"]

# Printing before update
print("List 1 before update")
print(l1)

# Merge two list using extend
l1.extend(l2)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru']
List 1 after update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru', 'Mumbai', 'Pune']

 

Example 2 : Merge a list and a tuple using extend() method

In this example, we are having one list and one tuple containing city names. Here, we will append the content of list l1 with the tuple t1.

# Initializing the list and tuple
l1=["Delhi","Bhopal","Hyderabad","Bengaluru"]
t1=("Mumbai","Pune")

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to merge list and a tuple
l1.extend(t1)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru']
List 1 after update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru', 'Mumbai', 'Pune']

 

Example 3 : Merge a list and a dictionary using extend() method

In this example, we are having one list and one dictionary containing city names. Here, we will append the content of list l1 with the dictionary d1. Note that only keys will be appended to the list and not the values.

# Initializing the list and dictionary
l1=["Delhi","Bhopal","Hyderabad","Bengaluru"]
d1={"Mumbai":22,"Pune":21}

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to merge list and a dictionary
l1.extend(d1)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru']
List 1 after update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru', 'Mumbai', 'Pune']

 

Example 4 : Merge a list and a String using extend() method

In this example, we are having one list and one string containing city names. Here, we will append the content of list l1 with the string s1. As we can see, each characters from the string is appended as an individual element in the list.

# Initializing the list and string
l1=["Delhi","Bhopal","Hyderabad","Bengaluru"]
s1="Mumbai"

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to merge list and a string
l1.extend(s1)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru']
List 1 after update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru', 'M', 'u', 'm', 'b', 'a', 'i']

 

Example 5 : Merge a list and a set using extend() method

In this example, we are having one list and one set containing city names. Here, we will append the content of list l1 with the set s1.

# Initializing the list and set
l1=["Delhi","Bhopal","Hyderabad","Bengaluru"]
s1={"Mumbai", "Pune","Chennai"}

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to merge list and a set
l1.extend(s1)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru']
List 1 after update
['Delhi', 'Bhopal', 'Hyderabad', 'Bengaluru', 'Mumbai', 'Pune', 'Chennai']

 

Example 6 : Merge a list of tuples and list using extend() method

In this example, we are having one list of tuples and one normal list containing the name of the students. Here, we will append the content of list l1 with the list l2.

# Initializing the list of tuples with the other list elements
l1=[(1,"Raj"),(2,"Ram"),(3,"John"),(4,"Kim")]
l2=["Roy","Risha","Jeny","Kiya"]

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to merge list of tuples and other list
l1.extend(l2)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
[(1, 'Raj'), (2, 'Ram'), (3, 'John'), (4, 'Kim')]
List 1 after update
[(1, 'Raj'), (2, 'Ram'), (3, 'John'), (4, 'Kim'), 'Roy', 'Risha', 'Jeny', 'Kiya']

 

Example 7 : Initialize the list with other list using extend() method

In this example, we are having one list and one set containing city names. Here, we will append the content of list l1 with the set s1.

# Creating an empty list and Initializing the list
l1=[]
l2=["Mumbai", "Pune","Chennai"]

# Printing before update
print("List 1 before update")
print(l1)

# Using extend to initialize the list
l1.extend(l2)

# Printing the result of merging
print("List 1 after update")
print(l1)

Output

List 1 before update
[]
List 1 after update
['Mumbai', 'Pune', 'Chennai']

 

Example 8 : Merge multiple list using extend() method

In this example, we are taking three list with the different type of values. In order to merge this three list, we have to use extend method twice. Firstly, we will merge l2 with l1. Thereafter, we will merge l3 with l1. So, the resultant list l1 will have the content of all the three lists.

# Creating three list with different types of values
l1=[10,20,30]
l2=[10.5,20.5,30.5]
l3=[11,21,31]

# Merging list l1 and list l2
l1.extend(l2)

# Merging list l1 and list l3
l1.extend(l3)

# Priniting the result of merging of three lists
print("List 1 after update")
print(l1)

Output

List 1 after update
[10, 20, 30, 10.5, 20.5, 30.5, 11, 21, 31]

 

Summary

The knowledge of extend method in list is very useful while working on real time applications in Python. In many situations, we will need to merge the content of multiple data structure to form the one. So, we can use extend method to merge the content of any other supported data structure with the list. In this tutorial, we covered different examples to merge the other data structures with list. As per the requirement of an application, we can choose an appropriate approach for merging. We learned in detail about this with an example.

All in all, this tutorial, covers everything that you need to know in order to have a clear view on using the extend method of list in Python.

 

References

Lists in Python

 

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

X