Difference Between append() and extend() in Python
Basis for Comparison |
Append() |
Extend() |
---|---|---|
Purpose |
To add a single entry to the end of a list, use the append() function. | To add additional elements or an iterable to the end of a list, use the extend() function. |
Input |
accepts only one input element. |
accepts as input an iterable (such as a list or tuple). |
Operation |
The append() function adds the full input to the list as a single item. |
extend() adds each item to the list independently after iterating through each one in the input. |
Efficiency |
Since append() only executes one operation, it is typically quicker and more effective than extend(). |
When adding elements from numerous iterables or with huge inputs, extend() could take longer. |
Time complexity |
Append has constant time complexity i.e.,O(1) |
Extend has a time complexity of O(k). Where k is the length of the list which need to be added. |
append() and extend() in Python
Extend and Append are two Python list methods used to add elements to a list. Although they appear similar, they have different functionalities and use cases. Understanding the differences between the append() and extend() methods is crucial when working with lists in Python. Although both techniques are used to add elements to a list, their behaviors and effects vary. We will examine the distinctions between append() and extend(), how they are used, and when to pick one over the other in this post.
Contact Us