Why don't you just create a new one?

lang = {'en': lang['en']}

Edit: Benchmark between mine and jimifiki's solution:

$ python -m timeit "lang = {'ar':'arabic', 'ur':'urdu','en':'english'}; en_value = lang['en']; lang.clear(); lang['en'] = en_value"
1000000 loops, best of 3: 0.369 usec per loop

$ python -m timeit "lang = {'ar':'arabic', 'ur':'urdu','en':'english'}; lang = {'en': lang['en']}"
1000000 loops, best of 3: 0.319 usec per loop

Edit 2: jimifiki's pointed out in the comments that my solution keeps the original object unchanged.

Answer from Fabian on Stack Overflow
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ remove key from dictionary python
remove key from dictionary python | Scaler Topics
May 4, 2023 - Level up your programming game with our Python course - the ultimate path to mastering one of the most in-demand languages in the tech world. ... We can use the clear() method of the dictionary class to remove all the keys (and associated values) from a given dictionary.
Discussions

python 3.x - Remove all keys excepts those mentioned from a dict python3 - Stack Overflow
i have a nested python dictionary where i am trying to remove all keys except those mentioned. I found a solution here where we can remove keys specified, what i am trying to do is reverse of it, i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 26, 2019
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
How common is the need of getting a copy of a dict, except some of its keys? It happens to me quite frequently that I need a similar dict to what I have, but without some of its keys. Of course I canโ€™t just remove the keys, as other parts of the code still uses the full dict. More on discuss.python.org
๐ŸŒ discuss.python.org
1
October 31, 2019
Is there a way to remove items/keys from a dict in a loop?
d = { ... } for k in list(d.keys()): d.pop(k, None) The important bit is the list(d.keys(). Instead of iterating over the dict itself, that creates a new separate list of keys and iterates over that. More on reddit.com
๐ŸŒ r/learnpython
59
81
May 27, 2022
python - Removing multiple keys from a dictionary safely - Stack Overflow
Checking for membership is easier ... try: except: 2022-11-25T12:50:04.473Z+00:00 ... Save this answer. ... Show activity on this post. It would be nice to have full support for set methods for dictionaries (and not the unholy mess we're getting with Python 3.9) so that you could simply "remove" a set of keys... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-remove-key-from-dictionary
Python Remove Key from Dictionary โ€“ How to Delete Keys from a Dict
February 22, 2023 - You can use the clear() method to remove all key-value pairs from a dictionary. The syntax is as follows: ... For removing a key-value pair or several key-value pairs from a dictionary, we discussed a variety of Python methods in this article.
Top answer
1 of 3
1

You're looking for the intersection between the set of keys in the dict and the set of values in the list. i.e. all keys that exist in both sets.

Try this:

from collections import MutableMapping


d = { 'A': 1, 'B': 2, 'C': 3, 'D': 4 }
list_of_keys_to_keep = ['B', 'C', 'F']

def remove_fields(d, list_of_keys_to_keep):
    return {key: value for key, value in d.items() if key in list_of_keys_to_keep}

print(remove_fields(d, list_of_keys_to_keep)) # -> {'B': 2, 'C': 3}

Edit: Updated name of list_of_keys_to_remove to list_of_keys_to_keep since this seems to be what it actually represents.

Edit2: Updated after Askers update. The following works for the sample provided:

from collections import MutableMapping

d = {
  'G_1': [
    {
      'ACCOUNTING_RULE_ID': '1',
      'PAYMENT_TERM_ID': '4',
      'RENEWAL_TYPE': 'RENEW'
    },
    {
      'ACCOUNTING_RULE_ID': '2',
      'PAYMENT_TERM_ID': '4',
      'RENEWAL_TYPE': 'RENEW'
    },
    {
      'ACCOUNTING_RULE_ID': '3',
      'PAYMENT_TERM_ID': '4',
      'RENEWAL_TYPE': 'DO_NOT_RENEW'
    },
    {
     'ACCOUNTING_RULE_ID': '4',
      'PAYMENT_TERM_ID': '4',
      'RENEWAL_TYPE': 'RENEW'
    }
  ]
}
list_of_keys_to_keep = ['RENEWAL_TYPE']

def filter_dict(di, keys):
    if not isinstance(di, (dict, list)):
        return di
    if isinstance(di, list):
        return [filter_dict(value, keys) for value in di]
    return {key: filter_dict(value, keys) for key, value in di.items() if key in keys or isinstance(value, (dict, list))}

print(filter_dict(d, list_of_keys_to_keep))

Output:

{'G_1': [{'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'DO_NOT_RENEW'}, {'RENEWAL_TYPE': 'RENEW'}]}
2 of 3
0

try this one:

dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
keys_to_preserve = ["e", "b", "c"]

dict_final={k: v for k,v in dict.items() if k in keys_to_preserve}
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
October 31, 2019 - How common is the need of getting a copy of a dict, except some of its keys? It happens to me quite frequently that I need a similar dict to what I have, but without some of its keys. Of course I canโ€™t just remove the kโ€ฆ
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ remove multiple keys from python dictionary
Remove Multiple keys from Python Dictionary - Spark By {Examples}
May 31, 2024 - How to remove multiple keys from Python Dictionary? To remove multiple keys you have to create a list or set with the keys you want to delete from a
๐ŸŒ
thisPointer
thispointer.com โ€บ home โ€บ python โ€บ remove key from dictionary in python
Remove key from Dictionary in Python - thisPointer
May 24, 2023 - # If key exist in dictionary then delete it using del. key_to_be_deleted = 'where' try: del word_freq_dict[key_to_be_deleted] except KeyError: print(f'Key {key_to_be_deleted} is not in the dictionary') ... These were the 6 different ways to remove a Key from a dictionary in python. ... This site uses Akismet to reduce spam. Learn how your comment data is processed. ... To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-ways-to-remove-a-key-from-dictionary
Python - Ways to remove a key from dictionary - GeeksforGeeks
January 30, 2025 - Explanation: del() statement directly removes the key-value pair for "city" and does not return the value making it ideal when the value is not needed. Dictionary comprehension allows us to create a new dictionary without the key we want to remove.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-remove-all-the-elements-of-a-dictionary-in-Python
How to remove all the elements of a dictionary in Python?
And each of these elements is removed from the dictionary using the del operator. In this example, we will see how to remove all the elements of a dictionary using the del method and a loop. this_dict = { "companyname" : "Tutorialspoint", "tagline" : "simplyeasylearning"} print("Dictonary before ...
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: remove key from dictionary (4 different ways)
Python: Remove Key from Dictionary (4 Different Ways) โ€ข datagy
December 20, 2022 - Learn how to remove a Python dictionary key, using the pop method, the del keyword, and how to remove multiple Python dictionary keys.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to remove multiple keys from a python dictionary
How to Remove Multiple Keys from a Python Dictionary - Be on the Right Side of Change
September 14, 2022 - Approach: Iterate across all the keys in the dictionary except the ones that you want to delete using a dictionary comprehension and then store only the required key-values in the dictionary.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-remove-keys-with-k-value
Python - Remove Keys with K value - GeeksforGeeks
July 15, 2025 - dict() Conversion filter() function returns an iterable so dict() is used to convert it back into a dictionary resulting in res containing only key-value pairs where value is not equal to K. This approach can be helpful if you're working with defaultdict and need to maintain some default behavior ... from collections import defaultdict # Initial defaultdict with default value as int (0) and some key-value pairs d = defaultdict(int, {'a': 1, 'b': 2, 'c': 1, 'd': 3}) K = 1 # Value to be removed from the dictionary # Dictionary comprehension to create a new dictionary excluding keys with value equal to K res = {key: value for key, value in d.items() if value != K} print(res)
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ how to delete key from dictionary in python
How to Remove One or Multiple Keys From a Dictionary in Python | Delft Stack
February 2, 2024 - The original dictionary is: {'Article': ... key from dictionary in python', 'Website': 'DelftStack.com'} Here, if the key is present in the dictionary, the del statement simply removes the key from the dictionary.
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ efficiently-removing-keys-from-python-dictionaries-5edc9a4ab9b3
Remove Keys from Python Dictionaries Effectively
August 24, 2024 - The first script uses the dictionary.pop(key, None) method, which attempts to remove the specified key from the dictionary. If the key is not found, it returns None instead of raising a KeyError.