The implementation of OrderedDict.__delitem__ in Python 3.7 is as follows:

def __delitem__(self, key, dict_delitem=dict.__delitem__):
    'od.__delitem__(y) <==> del od[y]'
    # Deleting an existing item uses self.__map to find the link which gets
    # removed by updating the links in the predecessor and successor nodes.
    dict_delitem(self, key)
    link = self.__map.pop(key)
    link_prev = link.prev
    link_next = link.next
    link_prev.next = link_next
    link_next.prev = link_prev
    link.prev = None
    link.next = None

This code does 3 things:

  • Remove an item from the internal key-value dictionary.
  • Remove a node from the dictionary holding linked list nodes.
  • Delete an item from a doubly linked list.

Since the average case complexity of all the above operations is constant, the average case complexity of OrderedDict.__delitem__ is constant as well.

Do note however that the worst case complexity of deleting a key from a dictionary is O(n), so the same applies for ordered dictionaries as well.

Answer from Agost Biro on Stack Overflow
Discussions

How can I remove a key from a Python dictionary? - Stack Overflow
I want to remove a key from a dictionary if it is present. I currently use this code: if key in my_dict: del my_dict[key] Without the if statement, the code will raise KeyError if the key is not More on stackoverflow.com
🌐 stackoverflow.com
How to get any element in a dictionary without a key, O(1) time complexity - Page 3 - Scripting Support - Developer Forum | Roblox
I’d like to get any element in the dictionary without a key, O(1) time complexity. Example dictionary local dictionary = { Key1 = "Stop", Key2 = "Woke", Key3 = "Developers" } Is there a way to get any value, whe… More on devforum.roblox.com
🌐 devforum.roblox.com
April 23, 2024
python - Removing key/value pairs in list of dicts - Code Review Stack Exchange
I have a list of dicts that all have the same keys. If a key's value is None in all dicts then I want to remove them (A solution that creates a new dict is fine as well). I'm concerned about my B... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
September 7, 2017
how much time will it take to remove a key, value pair from a dictionary?
Fixed time (O(1)). If you need the exact value on your computer, run a benchmark. More on reddit.com
🌐 r/learnpython
9
6
June 23, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › time-complexities-of-python-dictionary
Time Complexities of Python Dictionary - GeeksforGeeks
July 23, 2025 - Copying a dictionary creates a shallow copy and its time complexity is O(n) as each key-value pair is duplicated. ... The clear() method removes all elements from the dictionary with a time complexity of O(1).
🌐
Medium
binarybeats.medium.com › python-dictionary-understanding-methods-with-examples-aa367ede8650
Python Dictionary: Understanding Methods with Examples | by Binary Beats | Medium
April 24, 2023 - The time complexity of these methods is O(n), where n is the number of key-value pairs in the dictionary, as they need to iterate over all the items in the dictionary. The pop() method removes and returns the value associated with the specified key.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-delete-items-from-dictionary-while-iterating
Python | Delete items from dictionary while iterating - GeeksforGeeks
July 11, 2025 - Python3 · # Creating a dictionary ... print(myDict) Output · {1: 'Geeks', 3: 'Geeks'} Time complexity: O(1) for removing the key-value pair using pop() method....
🌐
Pythondictionary
pythondictionary.org › home › dictionary methods
Python Dictionary Methods Reference | PythonDictionary.org | PythonDictionary.org
April 25, 2025 - # Remove all items from the dictionary ... to list if needed keys_list = list(person.keys()) # Result: ['name', 'age'] Time Complexity: O(1) for creating view, O(n) for conversion...
🌐
JOOS
joosjuliet.github.io › complexity_of_python_operations
python operations time complexity – JOOS
Reading time ~2 minutes · python sort 알고리즘은 Timsort이다. 참고자료 : [https://medium.com/@fiv3star/python-sorted-알고리즘-timsort-dca0ec7a08be]
Find elsewhere
🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity - Python Wiki
Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still. See dict -- the implementation is intentionally very similar. As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different!
🌐
Wander In Dev
wanderin.dev › python-interview › python-dictionaries
Python Dictionaries – Wander In Dev
October 6, 2024 - You can create a dictionary using ... This method removes all items from dict_1. This method has a constant time complexity of O(1)....
🌐
Quora
quora.com › What-is-the-time-complexity-of-checking-if-a-key-is-in-a-dictionary-in-Python
What is the time complexity of checking if a key is in a dictionary in Python? - Quora
What is the time complexity of the Python built-in sorted function? ... R. Drew Davis · I've become a big fan of Python for getting things done. · Author has 1K answers and 3.1M answer views · 11y · I agree with Heinrich Hartmann's answer: O(1). Keep in mind that big O notation is ignoring a (possibly large) "constant" factor. The time to compute the hash of the key value and to resolve any collisions is in that constant.
🌐
Roblox Developer Forum
devforum.roblox.com › help and feedback › scripting support
How to get any element in a dictionary without a key, O(1) time complexity - Page 3 - Scripting Support - Developer Forum | Roblox
April 23, 2024 - I’d like to get any element in the dictionary without a key, O(1) time complexity. Example dictionary local dictionary = { Key1 = "Stop", Key2 = "Woke", Key3 = "Developers" } Is there a way to get any value, whe…
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
January 30, 2025 - For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age': 3 min read Python - Insertion at the beginning in OrderedDict
Top answer
1 of 1
6

Any solution will have to read the values associated to each key of each dictionary; so you won't be able to drop under \$\mathcal{O}(n\times{}m)\$ where \$m\$ is the length of each dictionary. This is pretty much what you are doing, but the if k not in keep_keys call slows things a bit as it is \$\mathcal{O}(m)\$ when it could be \$\mathcal{O}(1)\$ by using a set or a dictionary.

If you change the keep_keys list into a set you simplify the logic a bit: as soon as you find a key whose value is not None you can add it into the set.

dicts = [{'a': 1, 'b': None, 'c': 4}, {'a': 2, 'b': None, 'c': 3}, {'a': None, 'b': None, 'c': 3}]
expected = [{'a': 1, 'c': 4}, {'a': 2, 'c': 3}, {'a': None, 'c': 3}]

keep_keys = set()

for d in dicts:
    for key, value in d.items():
        if value is not None:
            keep_keys.add(key)

remove_keys = set(d) - keep_keys

for d in dicts:
    for k in remove_keys:
        del d[k]

print dicts == expected

This code, as your original one, assume that there is at least one item in dicts; otherwise set(d) will generate an exception as the variable d is not defined yet.


But this code mixes the actual logic with some tests. You should wrap it in a function to ease reusability and put the testing code under an if __name__ == '__main__': clause:

def filter_nones(dictionaries):
    if not dictionaries:
        return

    keep_keys = set()

    for dict_ in dictionaries:
        for key, value in dict_.iteritems():
            if value is not None:
                keep_keys.add(key)

    remove_keys = set(dict_) - keep_keys

    for dict_ in dictionaries:
        for key in remove_keys:
            del dict_[key]


if __name__ == '__main__':
    dicts = [
            {'a': 1, 'b': None, 'c': 4},
            {'a': 2, 'b': None, 'c': 3},
            {'a': None, 'b': None, 'c': 3},
    ]
    expected = [
            {'a': 1, 'c': 4},
            {'a': 2, 'c': 3},
            {'a': None, 'c': 3},
    ]

    filter_nones(dicts)
    print dicts == expected
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - The remove() method removes an element from a list or dictionary by its value and does not return the value. The pop() method default time complexity is O(1), which is constant and efficient. Yes, the pop() method can also be applied to sets and bytearrays in Python.
🌐
Verve AI
vervecopilot.com › interview-questions › how-does-understanding-python-delete-from-dict-impact-your-interview-success
How Does Understanding Python Delete From Dict Impact Your Interview Success
August 28, 2025 - A: For deleting by key, del and pop() are both efficient (O(1) average time complexity). If you need to filter many items, creating a new dictionary with comprehension might be clearer and safer than repeated in-place deletions.
🌐
Abdullahslab
abdullahslab.com › 2023 › 12 › 05 › python-ordered-dict-constant-time-complexity.html
How does python’s ordered dict have constant time complexity for add, search, and delete?
December 5, 2023 - Python’s collections.OrderedDict (and the regular dict in python 3.7+) is a key-value data structure that allows for constant time search, add, and delete operations, AND it maintains the order in which items (key-value pairs) were inserted. How is this possible? Python uses 2 internal data structures for its dict. It uses a hash table, which we already know has constant time add, search, and remove operations, however it also uses a doubly linked list to maintain the order the items were inserted.
🌐
CodeRivers
coderivers.org › blog › python-dictionary-remove
Python Dictionary Remove: A Comprehensive Guide - CodeRivers
January 26, 2025 - However, if you need to remove multiple elements, creating a list of keys to delete first (as shown in the example above) and then iterating over that list can be more efficient than repeatedly modifying the dictionary during iteration. The popitem() method has a constant time complexity for removing an item, but the order of removal can be a factor depending on your application. Removing elements from a Python dictionary is an essential skill for any Python developer.
🌐
Better Stack
betterstack.com › community › questions › how-to-remove-key-from-python-dictionary
How to remove a key from a Python dictionary? | Better Stack Community
January 26, 2023 - You can also use the pop() method to remove a key-value pair from the dictionary and return the default value if the key is not found, like this: ... my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.pop('b', 'Key not found') print(value) # ...