Difference between del, remove and pop on lists

0 votes

>>> a=[1,2,3] 

>>> a.remove(2) 

>>> a [1, 3]

>>> a=[1,2,3] 

>>> del a[1] 

>>> a [1, 3] 

>>> a= [1,2,3] 

>>> a.pop(1) 2 

>>> a [1, 3] 

>>>

Is there any difference between the above three methods to remove an element from a list?

Aug 1, 2018 in Python by bug_seeker
• 15,300 points
18 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

es, remove removes the first matching value, not a specific index:

>>> a = [0, 2, 3, 2] 

>>> a.remove(2) 

>>> a [0, 3, 2]

del removes the item at a specific index:

>>> a = [3, 2, 2, 1] 

>>> del a[1] [3, 2, 1]

and pop removes the item at a specific index and returns it.

>>> a = [4, 3, 5] 

>>> a.pop(1) 3 

>>> a [4, 5]

Their error modes are different too:

>>> a = [4, 5, 6]

>>> a.remove(7)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: list.remove(x): x not in list

>>> del a[7]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError:

list assignment index out of range

>>> a.pop(7)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: pop index out of range

answered Aug 1, 2018 by Priyaj
• 56,140 points

Related Questions In Python

0 votes
1 answer

What is the difference between pop() and remove()?

Array elements can be removed using pop() ...READ MORE

answered 2 hours ago in Python by Nisa
• 700 points
9 views
0 votes
1 answer

Difference between pop and delete methods

I think you mean del() and not ...READ MORE

answered Jun 25, 2018 in Python by Hamartia's Mask
• 1,580 points
21 views
0 votes
1 answer

difference between lists and sets

There are a lot more differences such ...READ MORE

answered Jan 7 in Python by SDeb
• 10,940 points
12 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
58 views
+4 votes
6 answers
0 votes
1 answer

What is the difference between Python and IPython?

There are few differences between Python and ...READ MORE

answered Jul 26, 2018 in Python by Priyaj
• 56,140 points
160 views
0 votes
1 answer

What is the difference between re.search and re.match?

The theoritical approach can be this way, re.match is ...READ MORE

answered Aug 10, 2018 in Python by Priyaj
• 56,140 points
190 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.