Quick Reach
Python list append method
The append method of Python adds or appends an element to the existing list.
Syntax of append method
List_name.append(object/element)
Where object/element is the one to be appended to the list.
Examples of using append method
Below example shows how to add single element by using append method
1
2
3
4
5
6
7
8
9
|
# This is append method example
list_Py = [‘this’,‘is’,‘list’,‘chapter’]
print (“List before append:”,list_Py)
list_Py.append (‘explaning append method’)
print (“List after append:”,list_Py)
|
Output will be:
List before append: [‘this’, ‘is’, ‘list’, ‘chapter’]
List after append: [‘this’, ‘is’, ‘list’, ‘chapter’, ‘explaining append method’]
Python extend method
Python extend method also appends given to existing list. Difference between extend and append can be seen in example below. Inspite of an element, another list can also appended to existing list.
Syntax of extend
List_name.extend(elements or list)
Example of using extend
1
2
3
4
5
6
7
8
9
|
# This is extend method example
list_Py = [‘this’,‘is’,‘list’,‘chapter’]
print (“List before extend:”,list_Py)
list_Py.extend ([“explaining extend method”, ” with multiple elements!”])
print (“List after extend method:”,list_Py)
|
Output will be:
List before extend: [‘this’, ‘is’, ‘list’, ‘chapter’]
List after extend method: [‘this’, ‘is’, ‘list’, ‘chapter’, ‘explaining extend method’, ‘ with multiple elements!’
From output you can see the one of the difference between append and extend: Extend added elements separately in existing list whereas in case of append whole string in appended was taken as single element.
In extend if you enclose a string in quotes without square brackets and extend an existing list, each character will be treated as separate element of extended list. For example:
1
2
3
4
5
6
7
8
9
|
# This is extend method example
list_Py = [‘a’,‘b’,‘c’]
print (“List before extend:”,list_Py)
list_Py.extend(‘def’)
print (“List after extend method:”,list_Py)
|
Output will be:
List before extend: [‘a’, ‘b’, ‘c’]
List after extend method: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
If we display length of above example for both extend and append, the code and output will be as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
list_append = [‘a’,‘b’,‘c’]
print (“List before append:”,list_append)
print (“length before append = “, len(list_append))
list_append.append(“def”)
print (“List after append:”,list_append)
print (“length after append = “, len(list_append))
print (“##########################################”)
list_extend = [‘a’,‘b’,‘c’]
print (“List before extend:”,list_extend)
print (“length before extend = “, len(list_extend))
list_extend.extend(“def”)
print (“List after extend:”,list_extend)
print (“length after extend = “, len(list_extend))
|
Output will be:
List before append: [‘a’, ‘b’, ‘c’]
length before append = 3
List after append: [‘a’, ‘b’, ‘c’, ‘def’]
length after append = 4
##########################################
List before extend: [‘a’, ‘b’, ‘c’]
length before extend = 3
List after extend: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
length after extend = 6
Leave A Comment?