Computer Science, asked by kswara241, 25 days ago

Which of the following can be used to add an item at the end of the list ?​

Answers

Answered by IISLEEPINGBEAUTYII
3

Explanation:

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]

answered Jun 22, 2019 by Harshdeep Khatke

Thanks, @Harshdeep, that was a very clear explanation.

commented Jun 24, 2019 by Ishaan

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]

Similar questions