How to use the value that is returned using the pop command?


#1

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

<In what way does your code behave incorrectly? Include ALL error messages.>
Its not about the code. The difference between pop and del command is that pop returns the value. I have heard we can return value only in functions. How is the returned value using pop command used? how to call that value?

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

#2

Let’s say you want to remove an index in a list of 3000 randomly generated numbers. The user puts in an index to remove, and you remove it. Along with removing it, you also tell them what number they just removed.

print (n.pop(userInput))

Now, you’ve removed the value at that index, and have also provided the removed value. This can also be useful for maths programs in which removed values have some significance to the result of the program.


#3

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