The append() method is used to add a single element at the back of a list.

using the append() method

#a list of integers
mylist = [1, 2, 3, 4]

#add an element at the back
mylist.append(5)

print(mylist)

Lists are the most versatile core data types in Python, they are mutable meaning that they can be modified after creation. The list type defines several useful methods that can be used to add elements into a list, one such method is append().

Inserting an element at the back of a list is one of the most common list operations. The append() methods makes it possible to achieve this in a convenient manner. It takes a single element  as argument and adds it at the back of the list.

Syntax and basic usage

The syntax of the append() method is as shown below: 

lst.append(item)
item The item to be added to the list

The append() method takes a single argument, item, it then inserts the item at the back of the list. The method has no return value, it simply returns the default return value, None

Since lists are mutable, the append() method modifies the original list rather than creating a new list. After the item is inserted, the length of the list is incremented by 1.

add an element at the end of a list

mylist = ['Python', 'Java']
print("before: ", mylist)

mylist.append("Ruby") #append an item

print('after: ', mylist)
print(len(mylist)) #the length of the list

In the above example, we defined a  list i.e mylist with two items, we called the append() method with "Ruby" as the argument. As you can see from the output, "Ruby" is inserted at the far back of the list, and the length of the list increases by one.

Append lists and other iterables

Lists are heterogenous, this means that they support elements from varying types. Literally speaking, you can append to a list anything that you can assign to a variable i.e anything that has value including other lists.

In the following example, we use the append() method with a list of lists.

append  a list

mylist =  [['zero', 'one', 'two'], ['three', 'four', 'five']]
print('Before: ', mylist)

new_list = ['six', 'seven', 'eight']
mylist.append(new_list)

print('After: ', mylist)

In the above example:

  • We created a list of lists called mylist i.e mylist =  [['zero', 'one', 'two'], ['three', 'four', 'five']]
  • Created another list called new_list
  • We then called the append() method of mylist with new_list as the argument. i.e  mylist.append(new_list)
  • new_list is added at the back of mylist, as shown by the output.

append to a list of tuples

mylist = [('Python', 'Django'), ('Ruby', 'Rails')]

new_item = ('JavaScript', 'React')

mylist.append(new_item)

print(mylist)

In the above example we have a list of tuples, we used the append() method to add a new tuple at the end of the list. 

insert multiple items with append()

To append multiple items, we can call the append() method repeatedly in a  loop. In each iteration, the current item of the loop gets inserted at the back of the list. 

append() with a for loop

mylist = ['Python','Ruby']
print("Before: ", mylist)

new_items = ('JavaScript', 'C++', 'Java')
for item in new_items:
   mylist.append(item)

print('After: ', mylist)

The for loop above iterates through the elements of the new_items tuple. During each iteration, the append() method is called with the current items, until all the items are exhausted. At the end, all the items in the new_items tuple are appended at the back of the list.

append() vs insert()

The list also defines a method called insert() which similarly adds a single element to the list. However, while append() only allows an element to be inserted at the back of the list, the insert() method is much more flexible as it inserts an element at an arbitrary position in the list.

The first argument of the insert() method is the index at which the element will be inserted, and the second argument is the element itself.

lst.insert(index, item)

The item is inserted at the position specified by index. All element after that position are shifted to create room for the incoming element at that specified position.

using insert()

mylist = ['three', 'five', 'six']
print('Before: ', mylist)

new_item = 'four'
mylist.insert(1, new_item) #insert one

print("After: ", mylist)

In the above example, we created a list, i.e mylist,  then used the insert() method to add an element at index 1.

You can actually use the insert() method to add an element at the back of the list by using an index that is equal or larger than the length of the list.

append an element using insert() 

mylist = ['three', 'four', 'five']

new_item = 'six'
mylist.insert(len(mylist), new_item) #insert at the back

print(mylist)

Note that we used len(mylist) as the index, and the results is that the item is added at the back of the list.

From the above example, it is clear that the append() method is a specialized version of insert(). append() makes it easier and much more convenient to add an element at the back of the list without having to specify an index in the arguments.

The following snippet shows the previous example with append() instead of insert().

 with append()

mylist = ['three', 'four', 'five']

new_item = 'six'
mylist.append(new_item) #insert at the back

print(mylist)

From the above examples, we can conclude that;  append() is more convenient when we simply want to insert an element at the back of the list. While insert() should be used when we want to add an element at any other position of the list.

append() vs extend() 

As we saw in an earlier example, we can use append() to add multiple elements into a list through a loop. However, this can be inconvenient in some cases, and that is why we have the extend() method.

The extend() method takes an iterable object such as list, tuple, set, e.t.c as argument. It then inserts all the elements of the iterable at the back of the list. This is more convenient than calling the append() method repeatedly in a loop.

lst.extend(iterable)

All elements in the iterable object will be appended at the back of the target list.

mylist = [1, 2]
print('Before: ', mylist)

new_items = (3, 4, 5, 6)
mylist.extend(new_items) #call extend

print('After: ', mylist)

In the above example, we called the extend() method of mylist with a tuple(new_items) as the argument. The results is that all elements in new_items are added at the back of mylist. The same example, with append() would be less convenient,  as append() will have to be called with each item, as shown below:

mylist = [1, 2]
print('Before: ', mylist)

new_items = (3, 4, 5, 6)
for item in new_items:
  mylist.append(item)

print('After: ', mylist)

You should therefore use extend() when you want to add multiple elements at the back of a list at once and append() when only a single element needs to be added.