Python List Append Vs Extend – Differences Explained

Python lists provide multiple methods to add items to an existing list.

The main difference between the append and extend methods in python is the Append method appends a single item to the end of the list. In contrast, the extend method adds multiple items from an iterable.

This tutorial teaches you in detail about the differences between the append() method and the extend() method in Python lists and when it is appropriate to use them. You’ll also learn about the performance of each method.

List Append Method

The list append method adds a single item to the end of the list.

It mutates the same list object and doesn’t create any new list object while adding a new item.

It accepts only one object. The object can be a single integer or a collection such as a list, set or tuple.

Python list accepts different types of objects in a single list. When a collection is appended to the list using the append() method, it is appended as a single object. The objects are NOT unpacked and added one by one.

Code

The below code demonstrates how to use the append() method to add a single integer to the end of the list.

num_list = [10, 20,30,30,40,50]

num_list.append(60)

num_list

The item 60 is inserted at the end of the list.

Output

    [10, 20, 30, 30, 40, 50, 60]

Now let us see how to append a collection object to the list using the append() method.

Appending a List using Append Method

This section teaches you how to append a list of items to an existing list using the append() method.

A new list object is passed to the append() method, which appends the list as a single object at the end. It doesn’t unpack the list and adds it as a single item.

num_list = [10, 20,30,30,40,50]

num_list.append([60,70,80,90])

num_list

The new list is appended as a single list object, and now the list looks list a list of lists.

Output

    [10, 20, 30, 30, 40, 50, [60, 70, 80, 90]]

Appending a Set using Append Method

This section demonstrates how the append() method works when a set is passed as a parameter.

The complete set is appended to the list as a single item.

Code

num_list = [10, 20,30,30,40,50]

num_list.append({60,70,80,90})

num_list

Output

    [10, 20, 30, 30, 40, 50, {60, 70, 80, 90}]

This is how the append() method works.

List Extend Method

The list extend() extends the list with the items in the iterable passed as the parameter.

The extend() method accepts ONLY the iterables as a parameter. It’ll not accept parameters such as integers or Strings.

The iterable can be a list, set, or a tuple. An iterable is nothing but an object that is capable of returning its objects one at a time.

Like the append() method, the extend() method also mutates the same list object while extending and doesn’t create any new list-objects.

Code

The below code demonstrates how the extend() method unpacks the list objects and adds them to the existing list as a single item.

num_list = [10, 20,30,30,40,50]

num_list.extend([60,70,80])

num_list

Output

    [10, 20, 30, 30, 40, 50, 60, 70, 80]

Extending a List With Set

This section demonstrates how the extend() method works with the set as a parameter. Similar to the list parameter, the set items are unpacked and added to the existing list as a single item.

While using the set parameter, the order is NOT maintained. Because Python set is an unordered data structure.

Code

num_list = [10, 20,30,30,40,50]

num_list.extend({60,70,80})

num_list

Output

    [10, 20, 30, 30, 40, 50, 80, 60, 70]

Extending a List With Single item

Now let us see what happens when you pass an integer or String to the extend() method.

It throws an error that the object is not iterable. But the append method doesn’t throw this error.

num_list = [10, 20,30,30,40,50]

num_list.extend(60)

num_list

Output

    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    /var/folders/gb/19ly8vfs7t53plwx43wx1q3m0000gn/T/ipykernel_12904/76905774.py in <module>
          1 num_list = [10, 20,30,30,40,50]
          2 
    ----> 3 num_list.extend(60)
          4 
          5 num_list


    TypeError: 'int' object is not iterable

Performance

The time complexity of the append() and the extend() methods are:

  • Append has O(1) time complexity
  • Extend has a O(k) time complexity

But you need time complexity based on the use cases.
It is better to use the append() method when you want to insert a single item to an existing list, whereas use the extend() method when you want to add more items from an iterable to an existing list.

Code

The code below times the append() and the extend() operation.

Time for Append() method

To iterate an iterable and append the item to the list using the append() method takes more time.

import timeit

print(timeit.timeit(

"""   
num_list = [10, 20,30,30,40,50]

new_list = [60,70,80,90,100,200,300,400,500,600,700,800,900,1000,1001,1002,1003]

for item in new_list:

        num_list.append(item)
"""   
, number=10))

Output

    4.595799987328064e-05

Time for Extend() method

To use extend the list with an iterable takes less time.

import timeit

print(timeit.timeit(

"""   
num_list = [10, 20,30,30,40,50]

new_list = [60,70,80,90,100,200,300,400,500,600,700,800,900,1000,1001,1002,1003]

num_list.extend(new_list)
"""   
, number=10))

Output

    1.1624999842752004e-05

Why it is necessary to have both the functions append and extend

It is necessary to have both functions. Because, based on the use cases, the user can select which method they want to use.

If a user needs to add only a single item to a list, the append() method can be used.

If more items from an iterable need to be added to a list, the extend() method can be used. No looping through the iterable is necessary and gives better performance.

Conclusion

To summarise, you’ve learned the difference between the append() and the extend() method.

You’ve also learned about the performance of each method and when it is appropriate to use these methods.

It answers the question Python list append vs extend.

If you’ve any questions, feel free to comment below.

You May Also Like

Leave a Comment