what is the difference between append and insert in python lists

0 votes
when working in a list. if i want to add a value, what is the difference between append and insert?

can you show me an example as well?
Mar 15, 2019 in Python by Mohammad
• 3,230 points
69,759 views
Append adds to the end of the list, while insert adds in front of a specified index. Insert is slower when compared to append.

5 answers to this question.

+2 votes

There is a simple difference between append and insert in python list,
append method can be use for adding new element in the list only but by using insert we can add as well as can modify already occupied position.
append method takes one argument (which you have to insert in the list) while insert method takes two elements (first will be the position of element and second will the element itself), Below are examples for both methods use:

Use of Append:

list = [1,2,3,4,5]

list.append(6)

print(list) # [1,2,3,4,5,6]

Use of Insert:

list = [1,2,3,4,5]

list.insert(5, 10) # [1,2,3,4,5,10]

list.insert(1, 10) # [1,10,3,4,5]
 

You can insert element at any position, but till will be store just after the last element position. Reason behind this logic is list is store data in ordered format. 


list.insert(100, 50) # [1,2,3,4,5,10]

Hope this helps!!

If you need to know more about Python, join Python online course today.

Thanks!

answered Jun 23, 2019 by Harshdeep Khatke
Thanks, @Harshdeep, that was a very clear explanation.
0 votes

Append method:- Append method adds a new element at the end of the list. The length of the list increases by one

Example:- new_list = [ 'a','b']

                 new_list.append( 'c' )

                 print(new_list)

Output:- [ 'a','b','c' ]

You can also append another list in the list.

another_list = [7, 2, 4, 3]

new_list.append(another_list)

print(new_list)

Insert method:- Insert method is the method which inserts a given element at a given index in a list. 

Syntax:- 

list_name.insert(index, element)

Example:-

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

new_list.insert(4,10)

print(new_list)

Output:- [1,2,3,6,10,5,4]

answered Mar 19, 2020 by chirag
• 180 points
+1 vote

If we want to add an element in the List then we can use the append  method. using this element is add at last position of list but insert(a, x) function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective positions.

#Program:
>>> a=[1,2,3,4,5]
>>> a.insert(1,4)#insert value 4 at 1 position
>>> a
[1, 4, 2, 3, 4, 5]
>>> a.insert(10,9)#if previous position not present then insert
#function add element at last position
>>> a
[1, 4, 2, 3, 4, 5, 9]

#Program:
>>> breakfast=["coffee","tea","toast","egg"]
>>> breakfast.append("salad")
>>> print(breakfast)
['coffee', 'tea', 'toast', 'egg', 'salad']

answered Jun 7, 2020 by sahil
• 500 points
0 votes

The difference is that with append, you just add a new entry at the end of the list. With insert(position, new_entry) you can create a new entry exactly in the position you want.

The append method adds a new item to the end of a list. It is also possible to add a new item to the end of a list by using the concatenation operator. On the other hand, with concatenation, an entirely new list is created. ...

answered Dec 16, 2020 by Rajiv
• 8,870 points
0 votes

Append():

This function is used to modify an already existing list. Adds a new specific element at the end of the list.

Syntax: List_Name.append(item)

Insert():

This function also modifies an already existing list. The only difference between append() and insert() is that insert function allows us to add a specific element at a specified index of the list unlike append() where we can add the element only at end of the list.

Syntax: List_Name.insert(index, item)

Refer below example for better understanding

image

Editor: Eclipse Luna with Pydev extension.

answered Dec 16, 2020 by Gitika
• 65,770 points

Related Questions In Python

0 votes
1 answer

What is the difference between sets and lists in Python?

Lists can contain duplicates but sets cannot Sets ...READ MORE

answered Feb 9, 2022 in Python by Dev
• 6,000 points
1,139 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,020 points
2,392 views
+2 votes
2 answers

What is the difference between print and return in python?

Return statements end the execution of a ...READ MORE

answered Aug 26, 2019 in Python by anonymous
8,033 views
0 votes
0 answers

What is the difference between isdigit, isnumeric and isdecimal in python?

Can you give examples for each of ...READ MORE

May 31, 2019 in Python by Waseem
• 4,540 points
2,254 views
0 votes
2 answers

What is the difference between python lists and arrays?

Python arrays and lists have the same ...READ MORE

answered Jun 11, 2019 in Python by Nisa
• 1,090 points
1,544 views
0 votes
1 answer

What is the difference between str() and repr() functions in Python?

str() is mostly used to create output ...READ MORE

answered Jul 8, 2019 in Python by Arvind
• 3,050 points
1,611 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP