The pop method of dicts (like self.data, i.e. {'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments -- see the docs

The second argument, default, is what pop returns if the first argument, key, is absent. (If you call pop with just one argument, key, it raises an exception if that key's absent).

In your example, print b.pop('a',{'b':'bbb'}), this is irrelevant because 'a' is a key in b.data. But if you repeat that line...:

b=a()
print b.pop('a',{'b':'bbb'})
print b.pop('a',{'b':'bbb'})
print b.data

you'll see it makes a difference: the first pop removes the 'a' key, so in the second pop the default argument is actually returned (since 'a' is now absent from b.data).

Answer from Alex Martelli on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_dictionary_pop.asp
Python Dictionary pop() Method
Python Examples Python Compiler ... } car.pop("model") print(car) Try it Yourself » · The pop() method removes the specified item from the dictionary....
Top answer
1 of 4
138

The pop method of dicts (like self.data, i.e. {'a':'aaa','b':'bbb','c':'ccc'}, here) takes two arguments -- see the docs

The second argument, default, is what pop returns if the first argument, key, is absent. (If you call pop with just one argument, key, it raises an exception if that key's absent).

In your example, print b.pop('a',{'b':'bbb'}), this is irrelevant because 'a' is a key in b.data. But if you repeat that line...:

b=a()
print b.pop('a',{'b':'bbb'})
print b.pop('a',{'b':'bbb'})
print b.data

you'll see it makes a difference: the first pop removes the 'a' key, so in the second pop the default argument is actually returned (since 'a' is now absent from b.data).

2 of 4
31

So many questions here. I see at least two, maybe three:

  • What does pop(a,b) do?/Why are there a second argument?
  • What is *args being used for?

The first question is trivially answered in the Python Standard Library reference:

pop(key[, default])

If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.


The second question is covered in the Python Language Reference:

If the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form “**identifier” is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.

In other words, the pop function takes at least two arguments. The first two get assigned the names self and key; and the rest are stuffed into a tuple called args.

What's happening on the next line when *args is passed along in the call to self.data.pop is the inverse of this - the tuple *args is expanded to of positional parameters which get passed along. This is explained in the Python Language Reference:

If the syntax *expression appears in the function call, expression must evaluate to a sequence. Elements from this sequence are treated as if they were additional positional arguments

In short, a.pop() wants to be flexible and accept any number of positional parameters, so that it can pass this unknown number of positional parameters on to self.data.pop().

This gives you flexibility; data happens to be a dict right now, and so self.data.pop() takes either one or two parameters; but if you changed data to be a type which took 19 parameters for a call to self.data.pop() you wouldn't have to change class a at all. You'd still have to change any code that called a.pop() to pass the required 19 parameters though.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-pop-method
Python Dictionary pop() Method - GeeksforGeeks
July 11, 2025 - The Python pop() method removes and returns the value of a specified key from a dictionary. If the key isn't found, you can provide a default value to return instead of raising an error.
🌐
Programiz
programiz.com › python-programming › methods › dictionary › pop
Python Dictionary pop()
# create a dictionary marks = { 'Physics': 67, 'Chemistry': 72, 'Math': 89 } element = marks.pop('Chemistry') print('Popped Marks:', element) # Output: Popped Marks: 72
🌐
Jobtensor
jobtensor.com › Tutorial › Python › en › Dictionary-Methods-pop
Python Dictionary pop(), Definition, Syntax, Parameters, Examples | jobtensor
Intro · .PY Python Basics · .PY Python Advance · .PY Python NumPy · .PY Python Pandas · .PY Python SciPy · clear() copy() fromkeys() get() items() keys() pop() popitem() setdefault() update() values() The pop() method removes the specified item from the dictionary.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › dict › pop.html
pop — Python Reference (The Right Way) 0.1 documentation
If default is not given and key is not in the dictionary, a KeyError is raised. >>> d = {'a': 1, 'b': 2} >>> d.pop('a') 1 >>> d.pop('x', 'foobar') 'foobar' >>> d {'b': 2}
🌐
DigitalOcean
digitalocean.com › community › tutorials › pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
July 24, 2025 - In dictionaries, .pop(key, default) allows you to attempt to remove a key and retrieve its value, while providing a fallback if the key does not exist. This approach helps you avoid KeyError exceptions and makes your code more robust when dealing with optional or unpredictable data sources.
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - I write practical, industry-focused ... impact, helping professionals turn data into confident decisions. The pop() method removes and returns n element from lists or dictionaries....
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › dictionaries › .pop()
Python | Dictionaries | .pop() | Codecademy
May 23, 2022 - The .pop() method of a Python dictionary returns the value of a specified key, then removes the key-value pair from the dictionary. ... Looking for an introduction to the theory behind programming?
🌐
Learn By Example
learnbyexample.org › python-dictionary-pop-method
Python Dictionary pop() Method - Learn By Example
April 20, 2020 - If default is not specified and key is not in the dictionary, a KeyError is raised. ... This method not only removes the specified key, but also returns its value. D = {'name': 'Bob', 'age': 25} v = D.pop('age') print(v) # Prints 25
🌐
Tutorialspoint
tutorialspoint.com › python › python_dictionary_pop_method.htm
Python Dictionary pop() Method
The Python dictionary pop() method is used to remove an item with the specified key from a dictionary and return its value. ... You provide the pop() method with a key as its argument.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python dictionary pop() method
Python Dictionary pop() Method - Spark By {Examples}
May 31, 2024 - The pop() method of Python dictionary (dict) is used to remove the element from the dictionary by dict key and return the value related to the removed
🌐
Vultr Docs
docs.vultr.com › python › standard-library › dict › pop
Python dict pop() - Remove Key-Value Pair | Vultr Docs
November 7, 2024 - The pop() method in Python dictionaries is an essential tool for managing key-value pairs. It allows for the removal of a specified key from a dictionary and returns the value associated with that key.
🌐
TutorialKart
tutorialkart.com › python › python-dictionary-pop
Python Dictionary pop() - Example
April 9, 2023 - Python Dictionary pop(key[, default]) method removes the (key, value) pair and returns the value. But if the key is not present and default is passed as argument, pop() returns the default value.
🌐
edSlash
edslash.com › home › python tutorials – learn with edslash › pop() method in dictionary – python
pop() method in dictionary - Python - edSlash
September 21, 2024 - If the key exists, Python removes the key-value pair from the hash table and returns the value associated with the removed key. If the key does not exist, a KeyError is raised to indicate that the specified key is not present in the dictionary. ... In the above example, first we create a dictionary with some items and store it in my_dict variable. After creating a dictionary print the dictionary on the output screen. Then pop item(’d’:20) from the original dictionary using the .pop() method.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove an Item from a Dictionary in Python: pop, popitem, clear, del | note.nkmk.me
April 27, 2025 - In Python, you can remove an item (key-value pair) from a dictionary (dict) using the pop(), popitem(), clear() methods, or the del statement. You can also remove items based on specific conditions us ...
🌐
Medium
medium.com › @zeebrockeraa › how-to-use-python-dictionary-pop-method-easy-python-guide-3ac1987b6039
How To Use Python Dictionary Pop Method — Easy Python Guide | by Zeeshan Ali | Medium
June 26, 2023 - Pair of key and value will be removed of that specific key from dictionary. If you use only key and the key is found then it will delete this key value pair. But if not found, then it’ll show a key error exception. So for this reason, a default value can be passed as the second argument of pop method.
Top answer
1 of 2
1

dict.pop(v) works like list.remove(v) and dict.popitem() is similar to list.pop() and you can do del data[x] on bought but the __getitem__ in the dict evaluates an object instead a index and you can't access the items by the index but you can use the dict.keys() to translate an index to a value so you can override this functionality to work as a list with something like this:

class EDict(dict):

    def pop(self, index=-1):
        return dict.pop(self, tuple(self.keys())[index])

data = EDict({
    '1': 'a', '2': 'b',
    '3': 'c', '4': 'd',
})

if __name__ == '__main__':
    print(data)
    data.pop()
    print(data)
    data.pop(1)
    print(data)

(Amiga500 was probably referring to this post)

2 of 2
0

I don't have a definitive answer, but I have two thoughts on the subject.

  1. Three methods of Python dictionaries have optional default parameter: dict.get(), dict.setdefault() and, yes, dict.pop(). All of them essentially return value, associated with the key, so adding default to all of those is a consistent thing to do.

    Note that dict.popitem() doesn't accept the key, and consistently doesn't need defult.

  2. Another thing is basic convenience. Lists are sequences, and it's hard to imagine a case where you would need a default for list.pop(). If you try to pop an item from outside of list "bounds", you've got a bug, and papering over possible failure with list.pop(index, None) wouldn't do much good. On the other hand, it's quite common for dictionary to not contain a value for a specific key, so looking it up might very well be a normal situation.