(k := next(iter(d)), d.pop(k))

will remove the leftmost (first) item (if it exists) from a dict object.

And if you want to remove the right most/recent value from the dict

d.popitem()
Answer from Sudarshan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-pop-method
Python Dictionary pop() Method - GeeksforGeeks
July 11, 2025 - Explanation: while loop repeatedly removes and prints the first key-value pair from the dictionary using pop() until the dictionary is empty. Python Dictionary Methods · Python | Ways to remove a key from dictionary · Python Dictionary popitem() ...
Discussions

python 3.x - Pop element at the beginning or at the last from dict in python3 - Stack Overflow
Collection module in python has a datatype OrderedDict(), which enables us to save the key value pairs in the same order as they were inserted in dict. It has a method popitem, which allows us to pop More on stackoverflow.com
🌐 stackoverflow.com
python - Pop an element from a list in a dictionary - Stack Overflow
I have a dictionary where for each key there is a corresponding list. I am trying to create a function that receives the name of a key then removes and returns the first element in the correspondin... More on stackoverflow.com
🌐 stackoverflow.com
dictionary - In Python, what does dict.pop(a,b) mean? - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Access an arbitrary element in a dictionary in Python - Stack Overflow
for a non-destructive popitem you ... dict(d).popitem() 2018-01-31T10:35:00.843Z+00:00 ... Save this answer. ... Show activity on this post. If you only need to access one element (being the first by chance, since dicts do not guarantee ordering) you can simply do this in Python ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Programiz
programiz.com › python-programming › methods › dictionary › pop
Python Dictionary pop()
element = sales.pop('apple') print('The popped element is:', element) print('The dictionary is:', sales)
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-first-item-from-dictionary
Remove the First or Last item from a Dictionary in Python | bobbyhadz
April 10, 2024 - Remove the Last element from a ... next() function to get the first key in the dictionary. Use the dict.pop() method to remove the first key from the dictionary....
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - When we use the pop() method to remove the first or any other element, it works in O(n) time because it involves removing an element and shifting the other elements to a new index order. Check out our Analyzing Complexity of Code through Python tutorial to learn more about time complexity in Python.
🌐
W3Schools
w3schools.com › python › ref_dictionary_pop.asp
Python Dictionary pop() Method
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
Find elsewhere
🌐
Dr. Ehoneah Obed
blog.ehoneahobed.com › how-to-remove-the-first-item-in-a-python-dictionary
How to remove the first item in a Python Dictionary
May 23, 2023 - # Assuming you have a dictionary ... delete the item from the dictionary del my_dict[first_key] Alternatively, you can use the pop() method in place of the del function as shown below....
🌐
DigitalOcean
digitalocean.com › community › tutorials › pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
Learn how to use the .pop() method in Python to remove items from lists and dictionaries. Includes syntax, examples, differences from remove(), edge case…
🌐
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 ...
🌐
LearnModernPython
learnmodernpython.com › home › mastering the python dictionary pop() method: the ultimate guide
Mastering The Python Dictionary Pop() Method: The Ultimate Guide
April 7, 2026 - While pop() targets a specific key, popitem() removes and returns the last inserted key-value pair as a tuple. Since Python 3.7+, dictionaries maintain insertion order, so popitem() follows a LIFO (Last-In, First-Out) behavior.
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.

🌐
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.
🌐
TutorialKart
tutorialkart.com › python › python-dictionary-pop
Python Dictionary pop() - Example
April 9, 2023 - In this example, we will take a dictionary, and delete a key:value pair by passing key as argument to pop() method. ... myDictionary = {'a': 58, 'b': 61, 'c': 39} value = myDictionary.pop('b') print(value) print(myDictionary)
🌐
Finxter
blog.finxter.com › home › learn python blog › delete an element in a dictionary | python
Delete an Element in a Dictionary | Python - Be on the Right Side of Change
February 13, 2022 - Approach: The pop() method is used to remove an element from the dictionary with the specified key name.
🌐
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}
🌐
edSlash
edslash.com › home › python tutorials – learn with edslash › pop() method in dictionary – python
pop() method in dictionary - Python - edSlash
September 21, 2024 - 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.
🌐
Python Guides
pythonguides.com › get-first-key-in-dictionary-python
Get the First Key in a Python Dictionary
September 29, 2025 - Another clean and Pythonic way is to use an iterator. The iter() function creates an iterator over the dictionary keys, and next() retrieves the first element.