Using dict.pop:
d = {'some': 'data'}
entries_to_remove = ('any', 'iterable')
for k in entries_to_remove:
d.pop(k, None)
Answer from mattbornski on Stack OverflowUsing dict.pop:
d = {'some': 'data'}
entries_to_remove = ('any', 'iterable')
for k in entries_to_remove:
d.pop(k, None)
Using Dict Comprehensions
final_dict = {key: value for key, value in d.items() if key not in [key1, key2]}
where key1 and key2 are to be removed.
In the example below, keys "b" and "c" are to be removed & it's kept in a keys list.
>>> a
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
>>> keys = ["b", "c"]
>>> print {key: a[key] for key in a if key not in keys}
{'a': 1, 'd': 4}
>>>
Is there a way to remove items/keys from a dict in a loop?
Remove entries from dictionary with duplicate keys, without using a for loop
Remove multiple dictionary Keys
Shortcut to remove multiple keys from a dictionary
Videos
When I try to do this I get an error saying that the size of the dict has changed during iteration. Is it possible to achieve this without creating another dict?
Thanks!
I have a dictionary of HTTP codes to status text (404: Not Found, for example). Each status code has duplicates for the keys of the dictionary.
Is there a way to remove duplicate entries from the dictionary based on the key reoccurring without using a for loop? I'd also like to keep to base Python if possible (EG no Pandas DataFrame).
This isn't a project or question requirement, more so me developing my skills to use different tools and methodologies. A question of "is this possible?"
I've tried searching around for awhile and haven't found an answer. Mostly just converting lists to sets, which doesn't quite obtain my goal unless the key can be static with the value, not be shuffled, and converted back to a dictionary.
Edit: I'm home now and back to my laptop, those commenting that I was confused are right; the dictionary does not have duplicate keys. I may have mixed results between iterations of the code. Sorry!
Here is the code for anyone curious. All keys are integers, all keys are unique:
from bs4 import BeautifulSoup
from requests import get as reqGet
httpResponseCodes = BeautifulSoup(
reqGet(
"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status",
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
).text, "html.parser"
)
cleanedHTTPResponseCodes = [
element for element in [
elements.text.strip() for elements in httpResponseCodes.find_all("code")
] if any(char.isdigit() for char in element)
]
tempHTTPDictionary = {
int(element[0:3]): element[4:] for element in cleanedHTTPResponseCodes
}
print(tempHTTPDictionary)
for key in tempHTTPDictionary:
print(key)
print(type(key))This will be used for exception handling for API calls.
Hi everyone, as always, I’m pretty sure the solution is simple, but I can’t find any way to make it work...
I want to remove lines (keys) (based on manual selection) from a Dictionary (var "Old Chosen Paths" in this case).
My dictionary has 4 keys (var "Chose Offers" in this case) and I want to remove 2 of them (I have a few hundreds or dictionaries with changing number of Keys and Keys to remove, so that’s why I don’t want to remove it manually).
My initial dictionary looks like this:
Key1:Value1Key2:Value2Key3:Value3Key4:Value4
I thought about this:
-
Manually select Keys to remove (eg. 2)
-
Repeat with each
-
Replace “Key1” with “blank” in “Dictionary”
-repeat until no more keys to remove-
4. Get final Dictionary without 2 keys to be removed
(See screenshot)
The problem I get here is, it removes “Key1” from the initial Dictionary, then repeat the process which removes “Key2” also in the initial Dictionary. So I get a list of 2 dictionaries (first without “Key1” and second without “Key2”).
Result I get:Key2:Value2Key3:Value3Key4:Value4
ANDKey1:Value1Key3:Value3Key4:Value4
I would like the “Key2” (and others if necessary) to be removed from the previously edited Dictionary.
Expected result:
Key3:Value3Key4:Value4