Difference Between List's append() and insert() Methods in Python

Python List's append() Vs. insert() Methods: In this tutorial, we will learn about the append() and insert() methods and the difference between them. By Sanjeev Last updated : June 20, 2023

Python programming supports many inbuilt methods to work with lists. There are a variety of methods that can be used for inserting element(s) or list as an element to a list. Here, we will be learning about two popular methods append() and insert() with the help of examples and their difference.

What is Python List's append() Method?

The append() is an inbuilt method of the list class that is used to add an element/item to the list. The append() adds the element at the end. The method is called with this list and accepts an element to be added.

Syntax

The following is the syntax of append() method:

list.append(element)

Example

# Example of append() method

books = ["The Great Gatsby", "The Lord of the Rings"]

books.append("Jane Eyre")
books.append("The Hobbit")

print("List is:", books)

Output

List is: ['The Great Gatsby', 'The Lord of the Rings', 'Jane Eyre', 'The Hobbit']

What is Python List's insert() Method?

The insert() is an inbuilt method of the list class that is used to add an element /item at a specified index to the list. The insert() can add the element where we want i.e. position in the list, which we were not able to do the same in the append() Method.

Syntax

The following is the syntax of insert() method:

list.insert(index, element)

Example

# Example of insert() method

books = ["The Great Gatsby", "The Lord of the Rings"]

books.insert(0, "Jane Eyre")
books.insert(2, "The Hobbit")

print("List is:", books)

Output

List is: ['Jane Eyre', 'The Great Gatsby', 'The Hobbit', 'The Lord of the Rings']

Difference Between List's append() and insert() Methods

Both of the methods are used for inserting the elements into a Python list. The main difference between append() and insert() method is that append() appends an element to the end of the list. While insert() inserts an element at the specified index.

Example

The following example is demonstrating the difference of append() and insert() methods:

lst = [1, 2, 3, 4, 5, 6]

print('Original list:',*lst)

# appending two values in the
# existing list to show the
# working of append() function
lst.append(9)
lst.append(15)

print('\nList after appending 9 and 15')
print(*lst)

# inserting value 20 at index 2
lst.insert(2,20)

# inserting string literal 'include'
# at index 6
lst.insert(6,'include')

print('\nList after inserting 20 and "include"')
print(*lst)
print()

Output

Original list: 1 2 3 4 5 6

List after appending 9 and 15
1 2 3 4 5 6 9 15

List after inserting 20 and "include"
1 2 20 3 4 5 include 6 9 15

Differences Between append() and insert() Methods

S.No append() insert()
1 append() adds an element to the end of the list. insert() insert an element at the specified index.
2 append() can only append the elements to the existing list. insert() is able to insert as well as update the existing elements at the specified index.
3 append() accepts only one parameter that is the element to be appended. insert() accepts two parameters that are element to be inserted and the index where you want to insert that element.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.