Syntax:
list.extend(iterable)
Any iterable object is used as the argument value of this function. It changes the original content of the list and it returns nothing. Different uses of this function have been shown in the next part of this tutorial.
Example 1: Extend a List with Another List
Create a Python file with the following script that will extend the list with the values of another two lists. The first and the third lists contain all numbers. The second list contains all characters. The first extend() function will extend the first list with the values of the second list. The second extend() function will again extend the first list with the values of the third list. The modified content of the list will be printed in the output.
list1 = [3, 8, 6, 4, 9]
#Define the second list of strings
list2 = ['h', 's', 'w', 'b', 'r', 'o']
#Define third list of numbers
list3 = [39, 67, 23, 90, 45]
#Extend the first list with the second list
list1.extend(list2)
print("The content of the list after extending first time:")
#Print the extended list
print(list1)
#Extend the extended first list with the third list
list1.extend(list3)
print("The content of the list after extending second time:")
#Print the extended list again
print(list1)
Output:
The following output will appear after executing the above script.
Example 2: Extend the List with the String Value
Create a Python file with the following script that will extend the list with a string value. The string value works as a list of characters. The extend() function will extend the list with a string value. Next, the extended list will be printed.
listdata = [70, True, 56, 'Linux', 87, False, 'Ubuntu']
#Define a string
strdata = 'LinuxHint'
#Extend the list with the string data
listdata.extend(strdata)
#Print the extended list
print(listdata)
Output:
The following output will appear after executing the above script.
Example 3: Extend the List with the Set and Tuple
Create a Python file with the following script that will extend the list with the values of the set and the tuple by using the extend() function. The original list contains 5 elements. The first extend() function will extend the list with values of a set of 6 elements. The second extend() function will extend the modified list with the values of a tuple of 6 elements. So, the last modified list will contain 17 elements. The modified content of the list will be printed in the output.
listdata = [9, 7, 5, 1, 4]
#Define a set
setdata = {85, 89, 34, 12, 90, 75}
#Define a tuple
tupledata = (790, 290, 300, 450, 630, 460)
#Append set data to the list
listdata.extend(setdata)
print("The content of the extended list after adding set:")
print(listdata)
#Append tuple to the list
listdata.extend(tupledata)
print("The content of the extended list after adding tuple:")
print(listdata)
Output:
The following output will appear after executing the above script.
Example-4: Extend the List with the Dictionary
When a dictionary is used to extend the list, then the keys of the dictionary will be added with the list values. Create a Python file with the following script that will extend a list with a dictionary object. The list contains 5 numeric values. The dictionary contains 4 elements where the key is numeric and the value is a string. The extend() function will add the key values of the dictionary with the list values and the extended list will be printed later.
listdata = [9, 7, 5, 1, 4]
#Define a dictionary
dicdata = {'01':'Python', '02':'Perl', '03':'Bash', '04':'Java'}
#Append The dictionary data to the list
listdata.extend(dicdata)
print("The content of the extended list after adding dictionary:")
print(listdata)
Output:
The following output will appear after executing the above script.
Example-5: Difference Between the extend() and append() Function
The append() function is the alternative of the extend() function. It can be used to extend the content of the list but it adds the content of the iterable object like another list at the end of the main list. Create a Python file with the following script that will show how the extend() and the append() function works when adding values from the list to another list. Two lists of 5 elements and one list of 3 elements have been declared in the script. The third list has been added with the first list by using the extend() function, and the third list has been added with the second list by using the append() function. Both extended lists have been printed later.
listdata1 = [30, 41, 19, 29, 55]
#Define the second list
listdata2 = [80, 63, 75, 99, 69]
listdata3 = [56, 35, 72]
#Extending the first list using extend() function
listdata1.extend(listdata3)
print("The extended list data after using extend() function:")
print(listdata1)
#Extending the second list using append() function
listdata2.append(listdata3)
print("The extended list data after using append() function:")
print(listdata2)
Output:
The following output will appear after executing the above script. The output shows that the append() function has been added to the third list as a list item for the second list.
Conclusion
The ways of extending the list with a list, tuple, dictionary, and string have been shown in this tutorial by using the extend() function. The differences between the extend() and the append() functions have been shown also in the last part of the tutorial.