Why is remove used when we have del?


#1

Why is remove used when we have del?


#2

There are three methods for removing items from lists in Python, and which one you use depends on your purpose:

foo.remove(value)
Removes the first matching value rather than an index

del foo[index]
Removes a specific index

foo.pop(index)
Removes a specific index and returns the removed element


#3

The course doesn't tell us about pop. :stuck_out_tongue: anyway thx


#4

ambiguous this foo.pop command (index) Removes a specific index and returns the element removed as I remove and use, to that remove will use the same way found useless


#5

I'm afraid I don't quite follow you. What do you find ambiguous?

This simply means that the return value of the foo.pop() operation is the removed element.


#6

but has command that returns the value without need to remove it


#7

There are indeed, but these are all operations intended to remove the element.

There may well be a situation where you want to both remove the element and use it - for example, deleting an element from a list and displaying it to the user (so they can confirm which element was removed). This is simply a shortcut for doing so.


#8

do sense for python is what is server and interact with client what is the user


#9

I need help with the remove function
Instructions:
Remove 'dagger' from the list of items stored in the backpack variable.
Script.py:
backpack = ['xylophone', 'dagger', 'tent', 'bread loaf']
Response:
backpack.remove('dagger')
Error:
Oops, try again.
backpack has to be a list! Click Reset Code to get back to the original code.

What am I doing wrong?


#10

The code finally worked upon refreshing the page..Thanks ladies and gents


#11