Difference between Append, Extend and Insert in Python
Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects.
Lists are mutable, and hence, they can be altered even after their creation. Lists have various methods, the most commonly used list method are append(), insert(), extend(), etc. In this article, we are going to discuss the difference between append(), insert(), and, extend() method in Python lists.
Append
It adds an element at the end of the list. The argument passed in the append function is added as a single element at end of the list and the length of the list is increased by 1.
Syntax:
list_name.append(element)
The element can be a string, integer, tuple, or another list.
Example:
Python3
l = [ 'geeks' ]
l.append( 'for' )
l.append( 'geeks' )
print (l)
|
Output:
['geeks', 'for', 'geeks']
Insert
This method can be used to insert a value at any desired position. It takes two arguments-element and the index at which the element has to be inserted.
Syntax:
list_name(index,element)
The element can be a string, object, or integer.
Example:
Python3
l = [ 'geeks' , 'geeks' ]
l.insert( 1 , 'for' )
print (l)
|
Output:
['geeks', 'for', 'geeks']
Extend
This method appends each element of the iterable (tuple, string, or list) to the end of the list and increases the length of the list by the number of elements of the iterable passed as an argument.
Syntax:
list_name.extend(iterable)
Example:
Python3
l = [ 'hello' , 'welcome' , 'to' ]
l.extend([ 'geeks' , 'for' , 'geeks' ])
print (l)
|
Output:
['hello', 'welcome', 'to', 'geeks', 'for', 'geeks']
Difference between Append, Extend and Insert
append() |
insert() |
extend() |
The element passed as an argument is appended to the end of the list |
The element passed as the argument can be inserted at any desired position by passing the index along with it as a parameter. |
Each element of the iterable passed as an argument gets appended to the end of the list. |
An iterable passed as an argument appends without any change as a single element to the end of the list. |
An iterable passed as an argument appends without any change as a single element to the desired index of the list. |
An iterable passed as an argument will append each of its elements to the end of the list |
Length of the list increases by 1 |
Length of the list increases by 1 |
Length of the list increases by the number of elements in the iterable. |
Has a constant time complexity of O(1) |
Has a linear complexity of O(n). |
Has a time complexity of O(k) where k is the length of the iterable. |
Comparing the methods in a single program:
Python3
list_1 = [ 1 , 2 , 3 ]
list_2 = [ 1 , 2 , 3 ]
list_3 = [ 1 , 2 , 3 ]
a = [ 2 , 3 ]
list_1.append(a)
list_2.insert( 3 , a)
list_3.extend(a)
print (list_1)
print (list_2)
print (list_3)
|
Output:
[1, 2, 3, [2, 3]]
[1, 2, 3, [2, 3]]
[1, 2, 3, 2, 3]
Last Updated :
05 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...