Difference between Append, Extend and Insert in Python
Last Updated :
04 Jan, 2025
In Python, append(), extend() and insert() are list methods used to add elements to a list. Each method has different behaviors and is used in different scenarios.
Append
The append() method adds a single element which can be string, integer, tuple or another list to the end of the list. If we pass a list or another iterable, it will add the entire list as a single element.
Syntax:
list.append(element)
The element can be a string, integer, tuple or another list.
Example:
Python
li = ['geeks']
# use method
li.append('for')
li.append('geeks')
# display list
print(l)
Insert
The insert() method allows us to insert an element(string, object or integer) at any specified position (index) in the list. If the index is greater than the list's length, the element is added to the end. If the index is negative, it starts counting from the end of the list.
Syntax:
list.insert(index, element)
Example:
Python
li = ['geeks', 'geeks']
# use method
li.insert(1, 'for')
# display list
print(li)
Output:
['geeks', 'for', 'geeks']
Extend
The extend() method adds all the elements of an iterable (list, tuple, string) to the end of the list. Unlike append(), which adds a single element, extend() adds each element from the iterable to the list.
Syntax:
list.extend(iterable)
Example:
Python
# python program to demonstrate
# working of extend function
# assign list
li = ['hello', 'welcome', 'to']
# use method
li.extend(['geeks', 'for', 'geeks'])
# display list
print(li)
Output:
['hello', 'welcome', 'to', 'geeks', 'for', 'geeks']
Difference between Append, Extend and Insert
Parameter | append() | insert() | extend() |
---|
Purpose | Adds a single element to the end of the list. | Inserts an element at a specified index in the list. | Adds each element from an iterable to the end of the list. |
---|
Type of Argument | A single element (any data type, including a list). | An index (integer) and an element (any data type). | An iterable (list, tuple, string). |
---|
Location of Insertion | Always at the end of the list. | At the specified index position in the list. | At the end of the list (each element from the iterable). |
---|
Modifies the List | Yes, modifies the list in place. | Yes, modifies the list in place. | Yes, modifies the list in place. |
---|
Multiple Items | No, only one element is added at a time. | No, only one element is added at a specified position. | Yes, it can add multiple items from the iterable. |
---|
Iterables | It adds the list as a single element. | It can add a list, tuple, or any object at the specified index. | Adds elements from an iterable. |
---|
Similar Reads
Difference between append() and extend() in Python extend() and append() are two Python list methods used to add elements to a list but they behave quite differently. The append() adds a single item or any object, while extend() adds each element of an iterable to the list. In this article, weâll explore the differences between append() and extend()
2 min read
Difference Between '+' and 'append' in Python + Operator creates a new sequence by concatenating two existing sequences and .append() method (available for lists) modifies the existing list in place by adding one item at the end. In this article we are going to compare '+' operator and append in Python.+ Operator in Python+ operator is typicall
3 min read
Difference between + and , Python Print In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as
3 min read
Difference Between x = x + y and x += y in Python We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: Python3 x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: Python3 x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Outpu
2 min read
Difference between modes a, a+, w, w+, and r+ in built-in open function? Understanding the file modes in Python's open() function is essential for working with files effectively. Depending on your needs, you can choose between 'a', 'a+', 'w', 'w+', and 'r+' modes to read, write, or append data to files while handling files. In this article, we'll explore these modes and
4 min read