Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between append and extend in Python

Difference Between append and extend in Python

February 26, 2018 Posted by Lithmee

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].

Difference Between append and extend in Python

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.

Key Difference Between append and extend in Python

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?

  • Both append and extend are built-in functions provided by the Python programming language.

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

Related posts:

Difference Between Complete Binary Tree and Full Binary Tree Difference Between Machine Dependent and Machine Independent Code OptimizationDifference Between Machine Dependent and Machine Independent Code Optimization Difference Between Python and C LanguageDifference Between Python and C Language Difference Between Argument and ParameterDifference Between Argument and Parameter Difference Between Static Binding and Dynamic BindingDifference Between Static Binding and Dynamic Binding

Filed Under: Programming Tagged With: append and extend in Python Differences, append and extend in Python Similarities, append in Python, append in Python Definition, append in Python Function, append in Python List, append in Python Usage, append vs extend in Python, Compare append and extend in Python, extend in Python, extend in Python Definition, extend in Python Function, extend in Python List, extend in Python Usage

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

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

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

What is the Difference Between Berberine and Berberine HCl

What is the Difference Between Berberine and Berberine HCl

Difference Between Hydrogen Fluoride and Hydrofluoric Acid

Difference Between Hydrogen Fluoride and Hydrofluoric Acid

What is the Difference Between Saturated and Unsaturated Triglycerides

What is the Difference Between Saturated and Unsaturated Triglycerides

Difference Between Culture and Civilization

Difference Between Culture and Civilization

Difference Between Bowmans Capsule and Malpighian Capsule

Difference Between Bowmans Capsule and Malpighian Capsule

Latest Posts

  • What is the Difference Between Squalane and Hemi-squalane
  • What is the Difference Between Anencephaly and Microcephaly
  • What is the Difference Between Neuroleptic Malignant Syndrome and Serotonin Syndrome
  • What is the Difference Between Resolutions and Goals
  • What is Difference Between Tay-Sachs Disease and Niemann-Pick Disease
  • What is the Difference Between Enucleation and Marsupialization
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.