![]() |
VOOZH | about |
append() and extend() are two list methods used to add elements to a list, but they behave differently.
append() method adds a single element to the end of a list. This element can be a number, string, list, or any object.
list.append(element)
['geeks', 'for', 'geeks'] ['geeks', 'for', 'geeks', ['a', 'coding', 'platform']]
extend() method adds all elements from an iterable to the end of the current list. Unlike append(), it does not add the iterable as a single element; instead, each element is added individually.
list.extend(iterable)
['geeks', 'for', 6, 0, 4, 1]
Note: A string is also an iterable, so if we extend a list with a string, each character of the string is appended individually.
| Feature | append() | extend() |
|---|---|---|
| Purpose | Adds a single element to the end of the list. | Adds multiple elements from an iterable to the end of the list. |
| Argument Type | Accepts a single element (any data type). | Accepts an iterable (e.g., list, tuple). |
| Resulting List | Length increases by 1. | Length increases by the number of elements in the iterable. |
| Use Case | When we want to add one item. | When we want to merge another iterable into the list. |
Time Complexity | O(1) , as adding a single element is a constant-time operation. | O(k), where k is the number of elements in |