append () and extend () in Python

Loops | Python Methods and Functions | String Variables

my_list = [ 'geeks' , ' for' ]

my_list.append ( 'geeks' )

print my_list

Output:

 ['geeks',' for', 'geeks'] 

NOTE. List — it is an object. If you add another list to the list, the first list will be one object at the end of the list.

my_list = [ 'geeks' , ' for' , 'geeks' ]

another_list = [ 6 , 0 , 4 , 1 ]

my_list.append (another_list)

print my_list

 

Output:

 ['geeks',' for', 'geeks', [6, 0, 4, 1]] 

my_list = [ 'geeks' , ' for' ]

another_list = [ 6 , 0 , 4 , 1 ]

my_list.extend (another_list)

print my_list

 

Output:

 ['geeks',' for', 6, 0, 4, 1] 

NOTE. A string is iterative, so if you expand the list with a string, you add each character as you iterate over the string.

my_list = [ 'geeks' , 'for' , 6 , 0 , 4 , 1 ]

my_list.extend ( 'geeks' )

print my_list 

Output:

 ['geeks ',' for', 6, 0, 4, 1, 'g',' e', 'e',' k', 's'] 

Time complexity:
Add has constant time complexity, i.e. E. O (1). 
Expansion has a time complexity of O (k). Where k — the length of the list to add.