Answer: append() method is used to add a single element at the end of the list whereas extend() method adds multiple elements.
In Python, both append() and extend() methods are used to add elements to a list, increasing the indexes in the list. However, both methods are different based on their characteristics and functionality. Here’s a detailed explanation of their differences.
Table of Contents:
append() method in Python
The append() method in Python is used to add a single element to the end of the list, append() is the faster and simplest method to add the element one at a time.
Syntax:
list.append(item)
The list is where all the items are present, and the item is the element that you want to add to the list.
How it works: When you call append(item) on a list, the method adds the item to the end of the list. It does not modify the existing elements or change the order of the list except the size (because the size increases).
Example 1: A simple example of how the append() method works
Output:
Example 2: append() method for strings
Output:
In both examples, the new element added at the last of the list, append() modifies the list in place. This means it directly changes the original list and does not return a new list.
The time complexity for append() is O(1).
Note: In Python’s file handling, the append() method doesn’t apply in the same way as it does with lists. Instead, file handling in Python involves different methods for reading and writing to files, and append() is used when opening a file in append mode.
extend() method in Python
The extend() method in Python is used to add multiple elements to the end of the list, it is similar to append(), but append() adds a single element, whereas extend() takes an iterable (tuple, list, etc) and adds its elements to the list.
Syntax:
list.extend(iterable)
Note: The list is where all the items are present and the iterable (tuple, list..) elements that you want to add to the list. In Python, an iterable is an object that can return its elements one at a time.
How it works: Unlike append(), which adds the entire object as a single element, extend() adds the element separately to the end of the current list, and extend() does not return a new list but modifies the original list directly.
Example: A simple example of how the extend() works
Output:
Use of extend() method with Strings
Output:
Here, the string {‘intellipaat’, ‘lets’, ‘start’, ‘learning’} is treated as an iterable of characters, and each character is added individually to the existing list.
The time complexity of extend() is O(k).
Example: The below example shows the clear difference between append() and extend(). Given list [1,2] add another list [3,4] by using both append() and extend()
Output:
Difference Between append() and extend() Methods in Python
Append() | Extend() |
Adds a single element at the end of the list | Adds all elements of an iterable to the list |
Takes one element as an argument | Take one list as an argument |
The length of the list is increased by one | The length of the list is increased by the number of elements in iterable |
The time complexity is O(1) | The time complexity is O(K) |
Example: list1 = [1,2,3]list1.append(5) results in [1, 2, 3, 5] | Example: list1 = [1,2,3,4]list1.extend([5,6]) result in [1,2,3,4,5,6] |
Conclusion
The append() method in Python is used to add a single element to the end of the list, while the extend() method in Python is used to add multiple elements to the end of the list. append() treats the added item as a whole, whereas extend() adds each element individually.