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, 2019 in Python by Shabnam
• 930 points
4,330 views

1 answer to this question.

+1 vote

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, 2019 by Taj
• 1,080 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
• 58,100 points
2,338 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
• 58,100 points
5,037 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,370 points
2,078 views
0 votes
5 answers
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,391 views
0 votes
1 answer
+1 vote
4 answers

In Python, what is difference between Array and List?

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

answered Mar 15, 2019 in Python by Taj
• 1,080 points

edited Mar 18, 2019 by Omkar 152,689 views
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