Python

extend() method in list

Click for All Topics

extend() method in list - Python

  • This method is used to add any number of elements to a list from an iterable.
  • It adds all element of iterable to the list at the end.
  • Extend method modifies original list and it doesn’t return anything.
  • When we call the .extend() method with a list and pass an iterable as an argument then it iterates over the elements of the iterable and add elements of iterable one by one to the end of the list.
  • It takes O(k) time complexity when list has sufficient memory to store more element where k is the number of elements in an iterable.
  • It takes O(n) time complexity when list has insufficient memory to store iterable’s elements then list has to be resized in that case.
  • We can also do the same task using + operator in place of .extend() method .

 

Syntax –

list.extend(iterable)

 

Parameter –

  • iterable → any iterable element e.g. list, tuple, set, string, etc.
  • return value → None(it doesn’t return anything)

 

Example  1- Extend a list to the list

				
					my_list1=[11,22,33,44,55]
print("Original list: ",my_list1)
my_list2=[66,77,88,99,110]
my_list1.extend(my_list2)
print("Modified list: ",my_list1)

# Output -
# Original list:  [11, 22, 33, 44, 55]
# Modified list:  [11, 22, 33, 44, 55, 66, 77, 88, 99, 110]
				
			
  • In the above example, first we create a list(my_list1) with some elements.
  • After creating first list print the first list on the output screen.
  • And then create second list(my_list2) with some elements.
  • Then extend first list(my_list1) with element of second list(my_list2) using the .extend() method.
  • Finally, print the modified first list on the output screen.

 

Example 2 – extend a list with a string

				
					lst=['a','b','c','d','e']
my_string='fgh'
print("Original list:",lst)
lst.extend(my_string)
print("Modified original list:",lst)

# Output -
# Original list: ['a', 'b', 'c', 'd', 'e']
# Modified original list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
				
			

Example 3- extend a list with a tuple

				
					subject=['Hindi','English','Math']
subject_tuple=('Chemistry','Physics')
print("Original subject list: ",subject)
subject.extend(subject_tuple)
print("Modified subject list: ",subject)

# Output -
# Original subject list:  ['Hindi', 'English', 'Math']
# Modified subject list:  ['Hindi', 'English', 'Math', 'Chemistry', 'Physics']
				
			

Example 4- extend a list to the list using + operator

				
					my_lst1=['hello','Python','this']
my_lst2=['is','edSlash']
print("Original list: ",my_lst1)
my_lst1=my_lst1+my_lst2
print("Modified Original list: ",my_lst1)

# Output -
# Original list:  ['hello', 'Python', 'this']
# Modified Original list:  ['hello', 'Python', 'this', 'is', 'edSlash']
				
			
Difference between extend and append method of list in python?
extend() append()
It adds all elements of the iterable to the list it adds only single element to the list
If we pass a list as an argument to extend then it adds all elements of the list to the original list If we pass a list as an argument then it adds list itself to the original list as a nested list
It is useful when you want to add all elements of one list to another list. It is useful when you want to add a single element to the list or you want to add list as a single element to the list.