Online Tutorials & Training Materials | STechies.com
Register Login

append() and extend() in Python

30 Oct 2019 12:53 pm || 0

If you have a list of elements in Python, there might be a need to add more elements to it. The easiest ways to add elements to a list is by using the concatenation operator +. Using this operator you can add two or more lists into a new list. In case you want to add elements to the end of a list or before a specified index. Python offers methods such as append() and extend() for this.

Both append() & extends() functions are used for string manipulation, i.e. adding a new element to the list. But they have different behaviour.

Note: Both the function (append & extend) works with a number, strin, list, tuple, set, dict.

append() in Python

This function adds a single or group of elements at the end of a list as a single object. The length of the list increases by one.

Example of append() in Python

# Python program to explain append function
# Initialize string
mylist = [1,2,3]
print("Orignal list: ",mylist);
mylist.append([8,4])
print("Append list: ",mylist)
# Append single object
mylist = [1,2,3]
mylist.append(4)
print("Append single object: ",mylist)
# Append list with tuple
mylist = [1,2,3]
mylist.append(("apple", "banana", "cherry"))
print("Append tuple: ",mylist)
# Append list with set
mylist = [1,2,3]
mylist.append({"apple", "banana", "cherry"})
print("Append set: ",mylist)
# Append list to empty list
mylist = []
mylist.append(["apple", "banana", "cherry"])
print("Append to empty list: ",mylist)

Output:

Orignal list: [1, 2, 3]

Append list: [1, 2, 3, [8, 4]]

Append single object: [1, 2, 3, 4]

Append tuple: [1, 2, 3, ('apple', 'banana', 'cherry')]

Append set: [1, 2, 3, {'apple', 'banana', 'cherry'}]

Append to empty list: [['apple', 'banana', 'cherry']]

Explanation

Let us understand the code a little better. The first append statement adds the two elements [8,4] to the end of the list as a single element. Thus, mylist becomes [1, 2, 3, [8, 4]]. The next append statement adds a single element 4 to mylist. It becomes [1,2,3,4].

The third append statement adds a tuple to the existing list mylist. When this tuple is added to the end of the list, its value becomes [1, 2, 3, ('apple', 'banana', 'cherry')]. Then a set {"apple", "banana", "cherry"} is added to mylist. It becomes [1, 2, 3, {'apple', 'banana', 'cherry'}]. Then finally, mylist is assigned an empty list with no values. Then the list ["apple", "banana", "cherry"] is added to it using the append() method. This list is added as a single element at the end of mylist, although there are no other elements. The resultant list looks like [['apple', 'banana', 'cherry']]. 

extends() in Python

extends() function takes an argument from the iterable (strin, list, tuple, set, dict) and add each elements at the end of the list. The length of the final list increases by the number of elements present in the iterable argument.

Example of extend() in Python

# Python program to explain Extend function
# Initialize string
mylist = [1,2,3]
print("Orignal list: ",mylist);
# Extend list with list
mylist.extend([8,4])
print("Append list: ",mylist)
# Extend list with tuple
mylist = [1,2,3]
mylist.extend(("apple", "banana", "cherry"))
print("Append tuple: ",mylist)
# Extend list with Set
mylist = [1,2,3]
mylist.extend({"apple", "banana", "cherry"})
print("Append set: ",mylist)

Output:

Original list: [1, 2, 3]

Append list: [1, 2, 3, 8, 4]

Append tuple: [1, 2, 3, 'apple', 'banana', 'cherry']

Append set: [1, 2, 3, 'cherry', 'apple', 'banana']

Note: extend() function won't work with a single object.

Explanation

The extend() method works a little differently than the append() method. Here, a list [8,4] is added to the mylist variable that is initialized with the values [1,2,3]. The value of mylist will be [1,2,3,8,4]. Here we can see that the extend() method does not add an element as a single value at the end of a list. It adds each element to the list.

Then the tuple ("apple", "banana", "cherry") is added to mylist making its value [1, 2, 3, 'apple', 'banana', 'cherry']. Similarly a set having values {"apple", "banana", "cherry"} is also added to extend the list. We can see in the final output that all these values are added to the end of the variable mylist as individual elements. 

Difference Between append() and extend()

Here are some basic difference between append() and extend() in Python:

  • append() takes single element as an argument whereas extend() doesn't take single element as an argument
  • append() adds all argument as a single element to the end of a list whereas extend()  iterates over the argument and add each element of argument at the end of the list.
  • append() increases the size of list by 1 whereas extend() increases the size of the list by the number of elements in the iterable argument. 

Conclusion

If you have a list where you want to add a single element, the append() method is the best and easiest. For adding more elements to the end of a list, the extend() method is preferred.