Compare the Difference Between Similar Terms

Difference Between append and extend in Python

Key Difference – append vs extend in Python
 

Python is a popular general-purpose programming language. It is a high-level language so the syntax is easily understandable and readable by the programmers.  The most basic data structure in Python is a sequence. It is a set of elements. The starting element has the index zero, next one has the index one and so on. A list is a one built-in type sequence in Python. Various operations can be performed on lists such as slicing, adding, multiplying etc. Each element inside the list is separated by a comma. All the elements are enclosed in a square bracket. In programming languages such as C, Java, it is essential to store the same data type in an array. But in Python list, it is not necessary for all elements to be the same data type. Python language provides several built-in functions associated with lists. The programmer can use them in their programs. Two of them are, append and extend. This article discusses the difference between append and extend in python.The append method is mostly used to add one element to the existing list while the extend method is used to add multiple elements to the existing list. The key difference between append and extend in Python is that, append adds its arguments as a single element to the end of the list while the extend iterates over its arguments by adding each element to the list and extending it.

CONTENTS

1. Overview and Key Difference
2. What is append in Python
3. What is extend in Python
4. Similarities Between append and extend in Python
5. Side by Side Comparison – append vs extend in Python in Tabular Form
6. Summary

What is append in Python?

The append is a built-in function in Python. It adds a single element at the end of the list. According to the below program, the list1 contains three elements, which are 1,2 and 3. Using the append method, number 4 is appended to the list1. It is added at the end of the list. The output gives the list as [1,2,3,4].

Figure 01: append in Python

Here, the existing list is [1,2,3,4]. The elements 5 and 6 belong to another list. Using the append function, [5,6] are added to the list. That list is appended to the original list. The [5,6] is a single list element that is appended at the end of the list. Therefore, the append method can add only a single element to the list. Even though, the new list has two elements, all these are appended as a single element to the original list.

What is extend in Python?

The extend is a built-in function in Python. It is used to add multiple elements at the end of an already existing list. The functionality of the extend function is as follows.

Figure 02: extend in Python

According to the below program, the list1 contains three elements which are 1,2 and 3. Using the extend method, number 4 is extended to the list1. When using the extend method, 4 should be included in a list. Now the list1 is [1,2,3,4].  There is another list called list2. It consists of two elements. After extending list2 to list1, the output is [1,2,3,4,5,6]. The elements in the list2 are added as separate elements to list1.

What is the Similarity Between append and extend in Python?

What is the Difference Between append and extend in Python?

append vs extend in Python

The append is a built-in function in Python that is used to add its arguments as a single element to the end of the list. The extend is a built-in function in Python that iterates over its arguments adding each element to the list while extending it.
 Length of the List
When using append, the length of the list will increase by one. When using extend, the length of the list will increase by how many elements were passed in the argument.
Usage
The append is used to add a single element at the end of the existing list. The extend is used to add multiple elements at the end of the existing list.

Summary – append vs extend in Python

Python is a high level, general-purpose programming language. It is a popular programming language among programmers because of it is easily readable and understandable. Maintaining and testing Python programs are also easy. Python language provides many built-in functions. Therefore, the programmers can use them in the program without implementing them from the beginning. This article discussed two built-in functions such as, are append and extend. The difference between append and extend in Python is that, append adds its arguments as a single element to the end of the list while the extend iterates over its arguments adding each element to the list, extending it.

Reference:

1.Point, Tutorials. “Python Lists.” Tutorials Point, 8 Feb. 2018.  Available here