append(): In Python, when we will pass an argument to append() then it will be added as a single entity in the list. In other words, we can say that when we try to append a list into another list then that whole list is added as a single object to the other list’s end and hence the length of the list will be incremented by 1 only. Append() has fixed time complexity of O(1) Example: Let’s take an example of two lists as shown below. list1 = [“Alpha”, “Beta”, “Gamma”]
list2 = [“Delta”, “Eta”, “Theta”]
list1.append(list2)
list1 will now become: [“Alpha”, “Beta”, “Gamma”, [“Delta”, “Eta”, “Theta”]] The length of list1 will now become 4 after addition of second list as a single entity. extend(): In Python, when we will pass an argument to extend() then all the elements which are contained in that argument get added to the list or in other words the argument will be iterated over. So, the length of the list will be incremented by the number of elements which have been added from another list. extend() has time complexity of O(n) where n is the number of the elements in an argument which has been passed to the extend(). Example: Let’s take an example of two lists as shown below. list1 = [“Alpha”, “Beta”, “Gamma”]
list2 = [“Delta”, “Eta”, “Theta”]
list1.extend(list2)
list1 will now become: [“Alpha”, “Beta”, “Gamma”, “Delta”, “Eta”, “Theta”]. The length of list1 will now become 6 in this scenario.