Python List – append() vs. extend()

In this Python tutorial, we will discuss the differences between append() and extend() methods of list.

Table Of Contents

append() method of List in Python

The append() method of class list adds a given object at the end of an existing list. This object can be an int, float, string, tuple, list or any other Python object. At a time, the append() function adds only one element.

Syntax:

list.append(object)

It takes an object as an argument and adds the given object at the end of the list. This object can be an element, list, tuple or any other object.

After the append() call, length of the calling list object will be increased by 1. If we pass another list to the append() function as argument, then it will added as a whole object and the calling list will become nested list.

Let’s see some examples,

Example 1:

In this example, we will add an element to an existing list.

listOfNumbers = [4, 67, 89, 97, 66]

print("Actual List: ",listOfNumbers)
print("Total elements in the Actual List: ",len(listOfNumbers))

# Add element "thisPointer" to the existing list.
listOfNumbers.append("thisPointer")

print("Final List: ", listOfNumbers)
print("Total elements in the Final List: ", len(listOfNumbers))

Output:

Actual List:  [4, 67, 89, 97, 66]
Total elements in the Actual List:  5

Final List:  [4, 67, 89, 97, 66, 'thisPointer']
Total elements in the Final List:  6

Initially the list had only 5 numbers, then we added a string at the end of the list. Now the total number of elements in the list is 6.

Example 2:

In this example, we will add a new list at the end of an existing list.

listOfNumbers = [4, 67, 89, 97, 66]

print("Actual List: ",listOfNumbers)
print("Total elements in the Actual List: ",len(listOfNumbers))

# Add a list at the end of an existing list.
listOfNumbers.append([11, 12, 13, 14, 15])

print("Final List: ", listOfNumbers)
print("Total elements in the Final List: ", len(listOfNumbers))

Output:

Actual List:  [4, 67, 89, 97, 66]
Total elements in the Actual List:  5

Final List:  [4, 67, 89, 97, 66, [11, 12, 13, 14, 15]]
Total elements in the Final List:  6

When we pass a list as an argument in the append() function, it adds the whole list object at the end of the calling list object. List size will increase by one only.

extend() method of List in Python

The extend() method of class list, accepts an iterable sequence as an argument. It adds all the elements from the given sequence to the end of calling list object.

Syntax of extend()

list.extend(Iterable_sequence)

It extends the calling list object, by appending all elements from the given iterable sequence. The length of the calling list object will be increased by the number of elements in the given sequence.

Let’s see an example. Here, we will use the extend() function to add an element to the list. As extends() accepts a sequence as an argument, so if you want to add an element only, then you need to create a list from that element before passing it to the extend() function.

listOfNumbers = [4, 67, 89, 97, 66]

print("Actual List: ",listOfNumbers)
print("Total elements in the Actual List: ",len(listOfNumbers))

# Add an element at the end of an existing list
listOfNumbers.extend([11])

print("Final List: ", listOfNumbers)
print("Total elements in the Final List: ", len(listOfNumbers))

Output:

Actual List:  [4, 67, 89, 97, 66]
Total elements in the Actual List:  5

Final List:  [4, 67, 89, 97, 66, 11]
Total elements in the Final List:  6

The actual list was of length 5, then we added number 11 to the existing list. Now the total number of elements in the list is 6.

Example 2:

In this example, we will add all elements from a list to an another list.

listOfNumbers = [4, 67, 89, 97, 66]

print("Actual List: ",listOfNumbers)
print("Total elements in the Actual List: ",len(listOfNumbers))

newList = [11, 12, 13, 14, 15]

# Add all elements of newList at the end of an existing list
listOfNumbers.extend(newList)

print("Final List: ", listOfNumbers)
print("Total elements in the Final List: ", len(listOfNumbers))

Output:

Actual List:  [4, 67, 89, 97, 66]
Total elements in the Actual List:  5

Final List:  [4, 67, 89, 97, 66, 11, 12, 13, 14, 15]
Total elements in the Final List:  10

Here, we added all the elements of a new list at the end of an existing list.

Difference between append() and extend()

The append() method adds an object at the end of list, therefore list’s size increases by 1 only. Whereas, the extend() method can add all the items of a sequence at the end of an existing list. Therefore, after calling the extend(sequence) method, the size of existing list increases by the number of elements in the added sequence.

Similarities between append() and extend()

  1. Both append() and extend the methods won’t return anything.
  2. These methods updates the calling list object in place.

Summary

In this article, we discussed the differences between the append() and extend() methods of list class in Python. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top