4/18 - can someone explain the difference between returning and removing something


#1

It appears (to me) that returning and removing something is the same thing, but I am assuming that because there are two different ways to do them that they have different functions that I’m not picking up on.

https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/removing-elements-from-lists?action=lesson_resume

```python

1.pop(index) will remove the item at index from the list and return it to you:

n = [1, 3, 5]
n.pop(1)
# Returns 3 (the item at index 1)
print n
# prints [1, 5]
  1. del(n[1]) is like .pop in that it will remove the item at the given index, but it won’t return it:

del(n[1])
# Doesn't return anything
print n
# prints [1, 5]

#2

they are different, and we see this when we modify the pop code a little bit:

n = [1, 3, 5]
removed_item = n.pop(1)
# Returns 3 (the item at index 1)
print n
print removed_item
# prints [1, 5]

now we can see that pop returned the removed item and we captured it in removed_item variable

this is very useful if we need to do something with the removed item


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.