Question
Does the pop()
function on a dictionary just access the data or remove it?
Answer
The pop()
function will return the value for the key AND then remove the key/value from the dictionary. The following code example shows the results of performing a pop()
on a dictionary to remove the item indexed by key āCā.
letters = { "A": 10, "B": 20, "C": 30, "D": 40 }
value = letters.pop("C")
print(letters)
# {'A': 10, 'B': 20, 'D': 40}
8 Likes
is the .pop() in the dictionary a function or a method? how come in the lesson it specified as same with .get() method and here on the question and answer platform called function?
and can someone clarify difference between a function and a method?
Both .pop()
and .get()
are methods. Methods belong to a certain class, so can only be called on instance of that class. you can also recognize methods by the difference in calling style:
# calling print function
print("hello world")
# calling method
{}.get('a')
4 Likes