13. remove a Few Things


#1



I'm not stuck here: I just have question.
What are the basic differences between del and remove for dicts?

del dict_name[key_name]

dict_name.remove(item)


Replace this line with your code.


#2

Hello

https://www.codecademy.com/en/forum_questions/52dde12c7c82ca670200061a

https://discuss.codecademy.com/t/remove-item-vs-del-dict-name-key-name/31708/3

Hope that helps.


#3

Thanks for your quick reply.

So this means that *.remove(item) *addresses a specific item's name and
removes it, and* del *an index number inside the dictionary. Am I right?


#4

.remove() only works for list, not dictionary's.

for a list, .remove() removes the first instance of the item it can find. So if you would have:

aList = [1,2,3,2]
aList.remove(2)

you get [1,3,2] since remove removes the first two

To delete a key from the dictionary, you can't use remove(), you have too use del


#5

Hello

Remove - removes a matching value:

Example -

numbers = [0, 2, 2, 3]
numbers..remove(2)
number [0, 2, 3]

Delete - del removes a specific key.

number = [3, 2, 2, 1]
del number[1]

Did you notice that remove( ) is for lists only and del [ ] is for dictionaries.

A dictionary is similar to a list, but you access values by looking up a key instead of an index.
Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name.

Hope that helps.


#6

:smile:

Nice you got there first.


#7

Awesome! Thank you a lot. I got it now


#8

Question -

If you wanted to remove all the twos.
Would you type -

aList.remove(2,2)?

Thanks


#9

nope, you would have to loop over the list, use list comprehension, or slices, it depends on the situation which is best.


#10

Very interesting. Did not think of slicing. Very cool.
Thank you.


#11