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
# python program to demonstrate # working of append function # assign list l = [ 'Lazyroar' ] # use method l.append( 'for' ) l.append( 'Lazyroar' ) # display list print (l) |
Output:
['Lazyroar', 'for', 'Lazyroar']
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
# python program to demonstrate # working of insert function # assign list l = [ 'Lazyroar' , 'Lazyroar' ] # use method l.insert( 1 , 'for' ) # display list print (l) |
Output:
['Lazyroar', 'for', 'Lazyroar']
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
# python program to demonstrate # working of extend function # assign list l = [ 'hello' , 'welcome' , 'to' ] # use method l.extend([ 'Lazyroar' , 'for' , 'Lazyroar' ]) # display list print (l) |
Output:
['hello', 'welcome', 'to', 'Lazyroar', 'for', 'Lazyroar']
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
# Python program to demonstrate # comparison between the three methods # assign lists list_1 = [ 1 , 2 , 3 ] list_2 = [ 1 , 2 , 3 ] list_3 = [ 1 , 2 , 3 ] a = [ 2 , 3 ] # use methods list_1.append(a) list_2.insert( 3 , a) list_3.extend(a) # display lists 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]