Extend vs Append in Python | Difference between Append and Extend in Python [6 Major Key Differences]

If you want to learn how to work with .append() and .extend() functions in Python and understand their key differences(Extend vs Append in Python) then you are in the right place.

In this Python Tutorial, I will discuss the difference between the Python append and extend list methods in tabular form as well as individual.

I will also explain what is append() function is in a Python list with examples and what is extend() function in a Python list.

Also, with the help of an example, I will make you see which one is faster Extend or Append function in Python with single as well as multiple elements.

So, Let’s start with the tabular form, and see the key difference between append and extend functions in Python:

Extend vs Append in Python

Basis of Comparisionappend()extend()
Syntaxlist_name.append (element)list_name.extend (iterable)
InputThis accepts only one input element.But, this accepts input as iterable such as a list or tuple
FunctionalityThe append() method in Python adds a single element to the end of the existing list.While the extend() method in Python appends several items to a list as individual items at the end of the list.
OperationThe append () method adds the full input to the list as a single item.The extent() method adds each item to the list separately after iterating through each one in the input.
EfficiencyIt is typically quicker and more effective than extend() since append() only executes one operation at a time.When adding elements from different iterables or with large inputs, extend() can take a longer time.
Time Complexityappend() method has constant time complexity, 0(1).extend() method has time complexity of 0(K). Where k is the length of the list which needs to be added.
This is the list of the statements for extend vs append in Python.

Now, let’s go to the actual methods one by one and understand them in deep:

READ:  Python Subtraction Program

What is the append() method in Python?

In Python, the append() method is a predefined method in a list, used to add a single element to the end of the existing list. It can be called using the dot notation.

Let me make it more clear by showing examples using the append() method in Python. You can see the output in the below screenshot.

.extend vs .append in Python

Now, I will show you how to append multiple values in an existing list using the append() method in Python list.

To store the value that we want to add in a separate list, we can use a for loop to call the append() method in a list in Python as many times as needed.

Refer to the below image to see the result:

append and extend in python

What is the extend() method in Python?

The extend() method is a built-in method in Python list used to add each item to the list separately after iterating through each one in the input. It will called using dot notation.

To make you understand, I will show you an example using the extend() method in Python. Refers to the below screenshot, it gives the output:

python extend vs append

Which one is faster extend or append method in Python?

We will find out which method is faster extend or append by using the timeit function from the timeit module in Python. By using the timeit module we can find out the run time of the program.

Timeit is the built-in Python library. This module provides a simple way to find the execution time of a Python program.

READ:  How to Send email using Python?

For Example: I have executed a Python program using the append and extend method with a single value.

import timeit

def single_append(x, y):
    x.append(y)
temperature = [23, 21, 20, 25]

print(timeit.timeit('single_append(temperature, 32)', setup='from __main__ import single_append,temperature'))


def single_extend(x, y):
    x.extend(y)

temp = [23, 21, 20, 25]
more_temp = [34]
print(timeit.timeit('single_extend(temp, more_temp)', setup='from __main__ import single_extend,temp,more_temp'))

Output: You can see below from the Python code that to append a single value takes less time than to extend a single value in Python.

0.08402279997244477                     // Run time of single append value
0.10593359998892993                     // Run time of single extend value

Now, I will check which is fast to append multiple values or to extend multiple values in Python list.

Here, is the full code to append multiple values and to extend multiple values in Python.

import timeit

def multiple_append(x, y):
    for i in y:
        x.append(i)

temp = [23, 21, 20, 25]
more_temp = [23, 33, 43]
print(timeit.timeit('multiple_append(temp, more_temp)', setup='from __main__ import multiple_append,temp,more_temp'))


def multiple_extend(x, y):
    x.extend(y)

temp = [23, 21, 20, 25]
more_temp = [23, 33, 43]
print(timeit.timeit('multiple_extend(temp, more_temp)', setup='from __main__ import multiple_extend,temp,more_temp'))

Output: Here, you can see below from the Python code that to append multiple values takes more time than to extend multiple values in Python.

0.14341480005532503                      // Run time of append multiple value
0.09257480001542717                      // Run time of extend multiple value

Conclusion

I hope that you liked my article and found it helpful. Now you can work with the append() and extend() methods as I have explained what is extend vs append in Python with the help of some examples.

Moreover, I have explained which one is faster append or extend method in Python as well.

You may also like to read: