4. Removing elements from lists


#1

What does the first point mean by returning it to me?

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


#2

well, pop is a function, you use this function, you pass it a list, then pop removes the item at a certain index, and returns the list with the item removed

Shouldn't the return keyword be explained at this point in the course?


#3

What is means is that the actual item is deleted, but the result is returned. You do not have any power over it anymore, though, so watch that.


#4

@fiendclub,
From
https://docs.python.org/2/library/stdtypes.html

s.pop([i]) same as

x = s[i]; del s[i]; return x

The pop() method’s optional argument i defaults to -1,
so that by default the last item is removed and returned

You might also read
http://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists
http://stackoverflow.com/questions/252703/python-append-vs-extend

====================================

messages = ["message1","message2"]
for i in range(3,11):
   new_message="message"+str(i)
   messages.append(new_message)
   print(" in: {0}>>{1}".format(new_message,messages))
   print("out: {0}>>{1}".format(messages.pop(0),messages))
   print("==========")

output would be

     in: message3>>['message1', 'message2', 'message3']
    out: message1>>['message2', 'message3']
    ==========
     in: message4>>['message2', 'message3', 'message4']
    out: message2>>['message3', 'message4']
    ==========
     in: message5>>['message3', 'message4', 'message5']
    out: message3>>['message4', 'message5']
    ==========
     in: message6>>['message4', 'message5', 'message6']
    out: message4>>['message5', 'message6']
    ==========
     in: message7>>['message5', 'message6', 'message7']
    out: message5>>['message6', 'message7']
    ==========
     in: message8>>['message6', 'message7', 'message8']
    out: message6>>['message7', 'message8']
    ==========
     in: message9>>['message7', 'message8', 'message9']
    out: message7>>['message8', 'message9']
    ==========
     in: message10>>['message8', 'message9', 'message10']
    out: message8>>['message9', 'message10']
    ==========

OR

messages = ["message2","message1"]
for i in range(3,11):
   new_message="message"+str(i)
   messages.insert(0,new_message)
   print(" in: {0}>>{1}".format(new_message,messages))
   print("out: {0}>>{1}".format(messages.pop(-1),messages))
   print("==========")

output would be

     in: message3>>['message3', 'message2', 'message1']
    out: message1>>['message3', 'message2']
    ==========
     in: message4>>['message4', 'message3', 'message2']
    out: message2>>['message4', 'message3']
    ==========
     in: message5>>['message5', 'message4', 'message3']
    out: message3>>['message5', 'message4']
    ==========
     in: message6>>['message6', 'message5', 'message4']
    out: message4>>['message6', 'message5']
    ==========
     in: message7>>['message7', 'message6', 'message5']
    out: message5>>['message7', 'message6']
    ==========
     in: message8>>['message8', 'message7', 'message6']
    out: message6>>['message8', 'message7']
    ==========
     in: message9>>['message9', 'message8', 'message7']
    out: message7>>['message9', 'message8']
    ==========
     in: message10>>['message10', 'message9', 'message8']
    out: message8>>['message10', 'message9']
    ==========

#5

Yes the return keyword is explained but like many instructions and explanations it was worded badly.

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

The above sounds like the item that was removed from the list is returned.


#6

From the coding example you can clearly see this is not the case. You are always free to look up more documentation (from the official python documentation) if something isn't clear


#7

A post was split to a new topic: About python .pop() method