I want to understand the difference between delete, remove and pop, in Python.

+1 vote
As all the three mentioned above are used to remove an element from a list, I want to understand when to use which method?
Jun 7 in Python by Shabnam
• 920 points
42 views

1 answer to this question.

0 votes

Hi,

Lets look at all the methods one by on:

1) Remove

This method has nothing to do with index, it removes the first matching element present in the list.

Syntax: 

list.remove(value)

Input:

a=[2,6,7,2]
a.remove(6)
a

Output:

[2, 7, 2]

2) Delete

This method removes an element from specified index.

Syntax:

del list[index]

Input:

 a=[2,6,7]
 del a[1]
 a

Output:

[2, 7]

3) Pop

This method removes an element at a specified index and returns it.

Syntax:

list.pop[index]

Input:

a= [2,6,7]
a.pop(1)
a

Output:

[2, 7]

Difference:

  • Remove() deletes the matching element from the list whereas the del and pop removes the element present at specified index
  • Difference between pop and del is that pop returns the deleted value whereas del does not 

answered Jun 7 by Taj
• 1,040 points

Related Questions In Python

+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
• 56,900 points
239 views
+1 vote
1 answer

What's the difference between eval, exec, and compile in Python?

exec is not an expression: a statement ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 56,900 points
575 views
+7 votes
2 answers

I want to build a recommender system incorporating diversity and accuracy in the recommender engine.

I dont know what exactly you are ...READ MORE

answered Sep 24, 2018 in Python by slayer
• 29,170 points
162 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 278 views
0 votes
1 answer

In Python, what is difference between Array and List?

Lists and arrays are used in Python ...READ MORE

answered Mar 15 in Python by Taj
• 1,040 points

edited Mar 18 by Omkar 4,257 views