Get a skill gap analysis, personalised roadmap, and AI-powered resume optimisation.
Introduction
A list is a contiguous allocation of data. The elements in the list need not be of the same data types. Due to this reason, it is one of the best advantages of using lists in Python. This feature is also known as heterogeneous property. We can include different data types in lists: integers, strings, chars, etc. An example of a heterogeneous list can be [1, 2, 3, 4, ‘a’, “coding”].
The main difference between the append() and extend() functions in Python is how they add elements to a list.
append() adds a single element to the end of the list as a whole.
extend() adds multiple elements from an iterable (such as a list, tuple, or set) to the list.
In short, append() takes one item and adds it as-is, while extend() takes an iterable and adds its elements individually.
In this blog, we will discuss append() and extend() in detail, and at the end, we will see the difference between append and extend in Python.
What is Append() in Python?
The append() function is one of the built-in functions used to add an element to a list. The main point about the append() function is that the element can only be added at last using it. As lists are heterogeneous, the input argument for append can also be of any data type.
The append function makes changes in the original list instead of creating a copy of the list. Every time we call the append function, the length of the list increases by 1, this is one of the difference between append and extend in Python.
Syntax of append() function
the_list.append(element)
“the_list” is the list to which the element needs to be added. The element can be of any data type. The dot (.) in the syntax is also equally important as it specifies the list. Here, the function will be implemented to the list before the dot.
Examples of Python append() function
Example 1:
Python
Python
prime_list = [2,3,5,7,11]
prime_list.append(13)
print(prime_list)
You can also try this code with Online Python Compiler
In the above code, append is used to add a string “ninjas” to the given list; this shows that we can use the append() function for different data types.
The extend() function is a built-in function that adds multiple elements to a list. The main point about the extend is that the input is an iterable such as strings, lists, etc. It iterates over its argument and appends every element to the specified list. In the case of append(), it appends the iterable as a single element; this is a significant difference between append and extend in Python. As lists can be heterogeneous, the input argument for extend can also be of any data type.
The extend function makes changes in the original list instead of creating a copy of the list. Every time we use the extend function, the list's length increases by the length of the argument.
Syntax of Extend() Function in Python
the_list.extend(iterable_object)
“the_list” is the list to which the element needs to be added. The iterable_object can be a string, list, or tuple. The dot (.) in the syntax is also equally important as it specifies the list.
In the above code, extend is used to add [3, “three”] to the given list. This shows that extend() function can be used for heterogeneous lists, tuples, strings, etc.
We learned about append and extend in detail and learned their functions. Now, let's discuss some key points of the difference between append and extend in Python.
Difference between append() and extend() in Python: append vs. extend
In this section of the blog, we will compare the functions to add elements in a list, append and extend, about which we have learned in detail until now. Now it's time to discuss the difference between append and extend in Python.
Parameters
append
extend
Input argument
The input argument can be of different data types.
The input argument is iterable and can be of different data types.
Usage
It is used to add a single element in a list in single use.
It is used to add multiple elements in a list in single use.
Addition
The element as the argument is added to the end of the list.
Each element in the iterable argument is added to the end of the list.
Behavior with Iterable
An iterable argument will get appended as a single element in the list.
All elements of an iterable argument will be appended in the list.
List Length Increase
The length of the list increases by one after every use of the append() function.
The length of the list increases by the length of the argument after every use of the extend() function.
Time complexity
It has a time complexity of O(1).
It has a time complexity of O(len), where len is the length of the argument.
Key Differences Between append and extend in Python
Function Purpose:
append(): Used to add a single item to the end of a list.
extend(): Used to add each item from an iterable to the end of the list.
Handling of Existing Lists:
append(): Adds the entire argument as a single new element to the list.
extend(): Adds each element of the iterable to the list, effectively merging it with the list.
Impact on Nested Lists:
append(): Adds a nested list as a single element, resulting in a list within a list.
extend(): Flattens the nested list and adds its elements individually to the original list.
Use Case:
append(): Ideal for adding a single item such as an integer, string, or another list.
extend(): Ideal for combining lists or adding multiple items at once.
Frequently Asked Questions
What is the difference between append and extend in Python?
The append() adds the element passed as the argument to the end of the list, and the extend() adds every element of the argument to the end of the list. The time complexity of the append is O(1), whereas, for the extend, it is O(length of argument).
Is append faster than insert in Python?
Yes, append() is generally faster than insert() in Python. append() adds an element to the end of the list with a time complexity of O(1), while insert() can require shifting elements, resulting in a time complexity of O(n).
How does the extend() function works in Python?
The extend() function adds multiple elements to the end of a list in a single use. It iterates over the argument and adds every element to the end. The changes are made in the original list only.
What will be the output of append and extend if an iterable is passed as an argument?
If an iterable is given as input to extend() function, it adds every element of the argument to the list. But in the case of the attend() function, it will add the argument as a single element to the list.
Conclusion
append() and extend() both add elements to the end of a list, but append() adds its argument as a single item, while extend() adds each element from an iterable individually. This makes extend() suitable for merging lists or adding multiple elements at once.
We discussed their syntax with some examples, and in the end, we summarised the difference between append and extend in Python in a table.