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.
Extend vs Append Python List Methods
In Python, there are two ways to add elements to a list: extend() and append(). However, these two methods serve quite different functions. In append() we add a single element to the end of a list. In extend() we add multiple elements to a list. The supplied element is added as a single item at the end of the initial list by the append() method. When an Iterable is supplied as a parameter, the extend() method adds each element from the Iterable to the list’s end individually. It alters the initial list.
What is Append in Python?
Python’s append() function inserts a single element into an existing list. The element will be added to the end of the old list rather than being returned to a new list. Adds its argument as a single element to the end of a list. The length of the list increases by one.
Syntax of Python append()
Syntax: list.append(item)
Parameters:
- item: an item to be added at the end of the list, The parameter is mandatory and omitting it can give an error.
Returns: The method doesn’t return any value
Example 1: Here in this example, we have added an item to an existing list using Python.
my_list = ['geeks', 'for']
my_list.append('geeks')
print my_list
Output
['geeks', 'for', 'geeks']
NOTE: A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list.
Example 2: Here in this example, we have added another list to an existing list using Python.
my_list = ['geeks', 'for', 'geeks']
another_list = [6, 0, 4, 1]
my_list.append(another_list)
print my_list
Output
['geeks', 'for', 'geeks', [6, 0, 4, 1]]
What is extend() in Python?
The Python’s List extend() Iterates over its argument and adding each element to the list and extending the list. The length of the list increases by a number of elements in its argument.
Syntax of Python extend()
Syntax: list.extend(iterable)
Parameters:
- iterable: Any iterable (list, set, tuple, etc.)
Returns: None
Example 1: Here in this example we are extending a list into a existing list using Python.
my_list = ['geeks', 'for']
another_list = [6, 0, 4, 1]
my_list.extend(another_list)
print my_list
Output
['geeks', 'for', 6, 0, 4, 1]
NOTE: A string is iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string.
Example 2: Here in this example we are extending a given item into a existing list using python.
my_list = ['geeks', 'for', 6, 0, 4, 1]
my_list.extend('geeks')
print my_list
Output
['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
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). |
|
|
|
|
|
|
|
|
|
append() and extend() in Python – FAQs
What is the Difference Between append() and extend() Methods in Python?
In Python, both
append()andextend()are methods for adding elements to a list, but they do so in different ways:
append(): Adds its argument as a single element to the end of a list, increasing the length of the list by one. The argument can be any data type, andappend()adds the argument as it is, as a single item.extend(): Takes an iterable (e.g., another list, set, tuple) and appends each element of the iterable to the list, extending the list.Example of
append():my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list) # Output: [1, 2, 3, [4, 5]]Example of
extend():my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list) # Output: [1, 2, 3, 4, 5]
What is extend() in Python?
The
extend()method in Python is used to add multiple elements from an iterable to the end of a list. The elements in the iterable are added individually to the list, extending it.Usage Example:
my_list = ['a', 'b', 'c']
my_list.extend(['d', 'e', 'f']) # Adds elements of the list individually
print(my_list) # Output: ['a', 'b', 'c', 'd', 'e', 'f']
What is append() in Python?
append()in Python is used to add a single item to the end of a list. The item added byappend()remains in its original form, which means that it can be of any data type.Usage Example:
my_list = [1, 2, 3]
my_list.append(4) # Appends 4 as a single element
print(my_list) # Output: [1, 2, 3, 4]
Is extend() Faster Than append()?
When adding elements from an iterable to a list, using
extend()can be faster and more direct than usingappend()in a loop becauseextend()is optimized for adding elements in bulk. However, the performance difference might not be significant for small lists or simple operations.
What is the Difference Between Append and Extend in Python argparse?
In the context of
argparse, a module used for parsing command-line options, arguments, and sub-commands in Python,appendandextendrefer to actions for storing command-line arguments:
appendAction: This action stores each command-line argument as a new list item. It is useful when multiple occurrences of an argument can occur, and you want to capture all of them.extendAction: Introduced in Python 3.8, this action extends a list by iterating over its argument value, which is expected to be an iterable. This is particularly useful when each command-line argument is itself a list of values.Example in
argparse:import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--numbers', action='append', type=int)
args = parser.parse_args()
print(args.numbers) # Output example: [1, 2, 3] if --numbers 1 --numbers 2 --numbers 3Example in
argparse:parser.add_argument('--numbers', action='extend', type=int, nargs='+')
args = parser.parse_args()
print(args.numbers) # Output example: [1, 2, 3, 4, 5] if --numbers 1 2 3 --numbers 4 5These differences highlight the specific uses of
appendandextendin both general Python programming and in specialized modules likeargparse.


