In Python, we have the List data structure that is used to store multiple values in a single variable. The Python list is an alternative data structure to the array data structure supported by various other programming languages. Python list supports two similar functions, namely
append()
and
extend()
that you can use to insert new data values in an existing list. Although both the methods allow us to add new elements to a list, the working and usage of each method are different from the other. In this article, we will discuss the difference between the Python list append() method and extend() method. So, let's get started by discussing both the methods and their syntaxes individually.
Python List append() Method
The append() method or function is used to add new data objects to an existing list, and it always adds the new data objects at the end of the list. The append() method can accept a single data object as a parameter and add it to the end of a list. Also, the single data object could be anything, such as a string , a number, a dictionary, a list, or an object. append() syntax
list_name.append(data)
Example
>>> my_list = [2,3,4,5,6,7] >>> my_list.append(8) >>> my_list [2, 3, 4, 5, 6, 7, 8]
As you can see in the above example, the
append()
method added 8 to the end of the list
my_list
. As 8 is a single data value, it is added at the end of the list. Let us see what will happen if we add a list using the append method.
>>> my_list = [2,3,4,5,6,7] >>> my_list.append([2,3,4]) >>> my_list [2, 3, 4, 5, 6, 7, [2, 3, 4]]
When we pass the list as a parameter to the append method, the append() method adds the complete list as a single value to the existing list. As you can see in the above example the
my_list.append([2,3,4])
statement added the list
[2,3,4]
as the 7th element to list
my_list
.
Python List extend() Method
In general, the
extend()
method is similar to the
append()
method as it also adds a new element at the end of the list. However, unlike the
append()
method, the
extend()
method only accepts an iterable object as a parameter and adds all the elements from that iterable object to the existing list.
extend() syntax
list_name.extend(iterable object)
The iterable object could be a list, tuple, dictionary, string, or set. Example
>>> my_list = [2,3,4,5,6,7] >>> my_list.extend((8,)) >>> my_list [2, 3, 4, 5, 6, 7, 8]
When you want to add a single value to a list using the extend method, you need to specify the single value as a tuple, list, string, dictionary, or set. To make the single value a tuple or list, we need to specify the value inside the parenthesis or square bracket with a comma, i.e.
(8,)
. Also, putting a comma is very important because if we do not add a command,
(8)
will be treated as an integer and the extend method throws an error when it does not receive an iterable object.
extend method with list
>>> my_list = [2,3,4,5,6,7] >>> my_list.extend([8,9,10]) >>> my_list [2, 3, 4, 5, 6, 7, 8, 9, 10]
The extend method adds all the elements from the iterable object to the existing list and keeps the list scalar.
extend method with dictionary
When we pass a dictionary to the extend method, it will only add the dictionary keys to the list.
>>> my_list = [2,3,4,5,6,7] >>> my_list.extend({8:"eight", 9:"Nine"}) >>> my_list [2, 3, 4, 5, 6, 7, 8, 9]
extend method with the string
The extend method will break the string and add the individual character from the string to the list.
>>> my_list = [2,3,4,5,6,7] >>> my_list.extend("89") >>> my_list [2, 3, 4, 5, 6, 7, '8', '9']
Python List append() vs extend()
append () | extend () |
The append(data) method can accept any data value as a parameter. | The extend(iterable) method can only accept iterable objects as a parameter. |
The append() method adds the complete data object as the last element to the list. | The extend() method adds the elements from the iterable object to the list. |
>>> my_list = [1,2,3] >>> my_list.append(4) >>> my_list [1, 2, 3, 4] |
>>> my_list = [1,2,3] >>> my_list.extend((4,)) >>> my_list [1, 2, 3, 4] |
>>> my_list = [1,2,3] >>> my_list.append([4,5,6]) >>> my_list [1, 2, 3, [4, 5, 6]] |
>>> my_list = [1,2,3] >>> my_list.extend([4,5,6]) >>> my_list [1, 2, 3, 4, 5, 6] |
>>> my_list = [1,2,3] >>> my_list.append({4:"four", 5:"five"}) >>> my_list [1, 2, 3, {4: 'four', 5: 'five'}] |
>>> my_list = [1,2,3] >>> my_list.extend({4:"four", 5:"five"}) >>> my_list [1, 2, 3, 4, 5] |
>>> my_list = [1,2,3] >>> my_list.append("45") >>> my_list [1, 2, 3, '45'] |
>>> my_list = [1,2,3] >>> my_list.extend("45") >>> my_list [1, 2, 3, '4', '5'] |
Conclusion
By using the Python List
append()
and
extend()
method, we can add new elements at the end of an existing list. The
append()
method accepts a single object and adds it as a single entity to the end of a list. On the other hand, the extend() method accepts an iterable object and adds its elements to the list. We can use the extend() method when we do not want our list to be scalar. Hopefully, this article helped you understand the difference between the Python List append() and extend() method clearly. Still, if you have any doubts or queries, share them with us in the comments section below.
People are also reading:
Leave a Comment on this Post