Difference between append() and extend() in Python
Last Updated :
13 Dec, 2024
extend() and append() are two Python list methods used to add elements to a list but they behave quite differently. The append() adds a single item or any object, while extend() adds each element of an iterable to the list. In this article, we’ll explore the differences between append() and extend() with examples and use cases to help us understand when to use each method.
append() Method
append() method is used to add a single element to the end of a list. This element can be any data type, a number, a string, another list, or even an object.
List.appendExample:
Python
a = ['geeks', 'for']
# Append 'geeks' at the end of 'a'
a.append('geeks')
print(a)
# Append an entire list to 'a'
a.append(['a', 'coding', 'platform'])
print(a)
Output['geeks', 'for', 'geeks']
['geeks', 'for', 'geeks', ['a', 'coding', 'platform']]
extend() Method
extend() method is used to add all elements from an iterable (e.g., a list, tuple, or set) to the end of the current list. Unlike append(), which adds an entire iterable as a single element, extend() adds each element from the iterable to the list.
List.extendExample:
Python
a = ['geeks', 'for']
b = [6, 0, 4, 1]
# Add all element of 'b' at the end of 'a'
a.extend(b)
print(a)
Output['geeks', 'for', 6, 0, 4, 1]
Note: A string is also an iterable, so if we extend a list with a string, it'll append each character of the string to the current list.
Key difference between append() and extend()
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 b |
Related Articles:
Similar Reads
Difference Between '+' and 'append' in Python + Operator creates a new sequence by concatenating two existing sequences and .append() method (available for lists) modifies the existing list in place by adding one item at the end. In this article we are going to compare '+' operator and append in Python.+ Operator in Python+ operator is typicall
3 min read
Difference between + and , Python Print In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
3 min read
Difference Between x = x + y and x += y in Python We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: Python3 x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: Python3 x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Outpu
2 min read
Difference between Method Overloading and Method Overriding in Python Method Overloading and Method Overriding are two key concepts in object-oriented programming that help you manage methods in classes. Both concepts allow you to define methods in different ways, but they serve different purposes and behave differently. Method overloading provides flexibility with fu
3 min read
Difference between modes a, a+, w, w+, and r+ in built-in open function? Understanding the file modes in Python's open() function is essential for working with files effectively. Depending on your needs, you can choose between 'a', 'a+', 'w', 'w+', and 'r+' modes to read, write, or append data to files while handling files. In this article, we'll explore these modes and
4 min read