4. Removing elements from lists


#1

What's the difference between the two functions [n.pop(index), del(n[1])]. The text says that the function pop returns the value after removing it but the del function doesn't, how can I make use of such property for the pop function since the value of that index will be deleted, and what's the best situation to use each of the three functions ( pop, remove, del).
Thanks in advance.


#2

pop() is useful (though destructive) if we wish to cycle through a list, retrieve an element and remove it at the same time. We still have the contents of the element.

Take for instance a deck of cards, shuffled. Once a card is dealt we don't want it in the list, but we still need the card, So it is assigned to the 'hand', and also removed from the deck.


#3

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