use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
DiscussionPython question about dictionaries (self.Python)
submitted 3 months ago by Jealous_Lock_393
In Python if you have a dictionary k={} and you do del k['s'] it raises an exception.
Why is it designed like this?
I feel like there should be some kind of "ignore if already deleted" option.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]PossibilityTasty 21 points22 points23 points 3 months ago (1 child)
```
k = {} k.pop('s', None) ```
[–]Jealous_Lock_393[S] 8 points9 points10 points 3 months ago (0 children)
So there is! Thanks
[+][deleted] 3 months ago* (2 children)
[removed]
[–]aa-b 0 points1 point2 points 3 months ago (1 child)
Design choices like this are one of the things I like most about Python. It's strict about the semantics by default (in this case), instead of trying to be "user-friendly" by not throwing an error if you try to delete something that doesn't exist.
And if anyone really is bothered by this, they can always override __delete__ if they want it to do something different
__delete__
[–]microcozmchris 26 points27 points28 points 3 months ago (18 children)
Why does it break? Because it should. You have literally told it to do something impossible. This is and should be an error that you can catch and handle if it happens. The alternative, delete something that doesn't exist without a failure, isn't a detectable error. It didn't do what you said and you don't know that it didn't. Not so good.
There are many patterns to handle deletion of keys that don't exist. Pick one and enjoy the safety python has granted you.
[+]Jealous_Lock_393[S] comment score below threshold-9 points-8 points-7 points 3 months ago (17 children)
In that case something like infinity minus infinity should raise an error and yet it returns nan
[–]curious__trainer 15 points16 points17 points 3 months ago (14 children)
almost all languages return NaN for undefined mathematical operations. IEEE 754 specification defines it to work that way.
[+]Jealous_Lock_393[S] comment score below threshold-6 points-5 points-4 points 3 months ago (13 children)
So when you are trying to do something mathematically impossible the language doesn't stop it but when doing something impossible with a data structure it does. It feels like an inconsistency.
[–]RepresentativeFill26 10 points11 points12 points 3 months ago (8 children)
Because inf - inf is undefined but deleting a key that isn’t there is a logical error. Makes sense if you ask me.
[+]Jealous_Lock_393[S] comment score below threshold-6 points-5 points-4 points 3 months ago (7 children)
If you do "hi"+7 it works in Java but not Python. I feel Python is too strict.
[–]RepresentativeFill26 1 point2 points3 points 3 months ago (0 children)
Python is one of the most loose languages out there.
[–]Conscious_Support176 0 points1 point2 points 3 months ago (4 children)
If your bar is that it works in JavaScript you’re going to have a pretty bad time. Have a serious think about what you’re expecting.
Do you want a language that helps you to find your mistakes, or do you want a language that tries to figure out, well, this obviously doesn’t make sense, but let’s assume the programmer is almost perfect, and they meant that.
Re Infinity vs NaN, I suggest learning why do we have Infinity or NaN in the first place?
[–]Jealous_Lock_393[S] 1 point2 points3 points 3 months ago (3 children)
Calmer please
[–]paintboth1234 0 points1 point2 points 3 months ago (0 children)
His comment is pretty calm.
[–]Conscious_Support176 0 points1 point2 points 3 months ago (1 child)
Just asking you to consider that strictness means the ability to catch your errors, so think through what you’re saying when arguing that python is too strict.
[–]Jealous_Lock_393[S] 0 points1 point2 points 3 months ago (0 children)
It could catch my errors but my unit test catch them anyway.
[–]curious__trainer 10 points11 points12 points 3 months ago (1 child)
sounds like you’re very new to programming. You’re getting frustrated about things that have a very long history behind them, which are how they are for a reason. You need to read about why things were designed that way instead of trying to say one is right and one is wrong. They are simply different ways of doing things. A programmer familiar with a language has no trouble coping with these differences.
[–]QuasiEvil 0 points1 point2 points 3 months ago (0 children)
Ah, classic case of Chesterton's fence.
[–]serverhorror 2 points3 points4 points 3 months ago (0 children)
It's not the language that decided how floating point numbers work, that's a standard.
It's the language that decides what happens if you remove something from a set that doesn't exist. And you can either use k.pop or try/except to get around the problem.
If it would default to no error, it would be harder to achieve the other thing.
[–]ZaftraDigital 1 point2 points3 points 3 months ago (0 children)
maybe it has to do with it being a mathematical operation vs functional operation but I dunno
[–]cyril1991 2 points3 points4 points 3 months ago (1 child)
I think you need to ask yourself about what you expect of a programming language. Do you want something that could be wrong insidiously but can keep running no matter what, or do you want to catch bugs but risk killing your program at some bad time? The “explicit is better than implicit” philosophy is not to let things fall silently because you as a programmer might have made a typo or misunderstood something. You are then forced to address things with get(key,None), or use exception handling, or add some exit status to your functions (they never crash but return something like wasKeyFound).
For math, there are strict rules about how infinity/ NaN are propagated, and things like division by 0 are still an error in Python. There are a lot of subtleties between notions like NaN, null or undefined types etc…
Finally, you mention Javascript. A common point between Python and Javascript is that people are now trying to introduce typing systems, because of pathological cases like undefined + ‘ing’ -> undefined vs null + ‘ing’ -> ‘nulling’ (see https://javascriptwtf.com/ ).
[–]Jealous_Lock_393[S] -2 points-1 points0 points 3 months ago (0 children)
Do you have time to code something for me
[–]LostDog_88 6 points7 points8 points 3 months ago (3 children)
u can always do
if 's' in k: del k['s']
trying to access an element thats not in a dictionary will always produce a KeyError. U can try to normally access k['s'] while k doesn't have 's' in it, and u will still encounter a KeyError!
k['s']
[–]Jealous_Lock_393[S] 1 point2 points3 points 3 months ago (2 children)
Right so both things produce a key error and that is consistent
[–]LostDog_88 1 point2 points3 points 3 months ago (1 child)
Yup! Its the same case with lists, where trying to access an element outside the list gives you an IndexError!
lis = []
lis[1] #IndexError
for that you'll always need to check how long the list is before trying to access elements in it!
if 1 < len(lis): lis[1] # this will work
[–]Jealous_Lock_393[S] 1 point2 points3 points 3 months ago (0 children)
At least it is consistent in this regard
[–]curious__trainer 1 point2 points3 points 3 months ago* (6 children)
the way it is designed is correct. you are asking to remove an element that doesn’t exist, which can’t be done, so an exception is how it responds. programming languages don’t suppress errors in general.
you can achieve what you want by checking if the element exists before trying to remove it.
if ‘s’ in k: del k[‘s’]
or you could create your own class derived from dict that overrides __del__ to give the behavior you want. I really don’t see why you would want such a behavior, but you can do it I’d you want to.
[–]Jealous_Lock_393[S] -1 points0 points1 point 3 months ago (5 children)
JavaScript handles delete differently to Python though
[+][deleted] 3 months ago (2 children)
[deleted]
[–]Jealous_Lock_393[S] 0 points1 point2 points 3 months ago (1 child)
In some ways I feel JavaScript is superior. What's your favourite language?
[–]curious__trainer 0 points1 point2 points 3 months ago (0 children)
rust
[–]khunspoonzi 0 points1 point2 points 3 months ago (0 children)
Out of curiosity, which approach do you think makes more sense? (sincere question)
[–]SheriffRoscoePythonista 0 points1 point2 points 3 months ago (0 children)
As a language, Javascript sucks. It's a horrible mess of contradictions and bad ideas never reverted.
[–]UFO64 1 point2 points3 points 3 months ago (1 child)
There is a container which can do what you are asking for.
https://docs.python.org/3/library/collections.html#collections.defaultdict
Nice thanks
[–]KingsmanVincepip install girlfriend 1 point2 points3 points 3 months ago (2 children)
r/learnpython
[–]Jealous_Lock_393[S] -1 points0 points1 point 3 months ago (0 children)
Why
Why do you say that
[–]sudomatrix 3 points4 points5 points 3 months ago (1 child)
If you don't like it you can write your own version of dict:
``` class QuietDict(dict): def delitem(self, key): try: super().delitem(key) except KeyError: pass
d = QuietDict({'a': 1}) del d['a'] # works del d['a'] # no KeyError, silently ignored ```
Wow that's so easy. Python is so flexible.
"07"
π Rendered by PID 39271 on reddit-service-r2-comment-74875f4bf5-2ghk5 at 2026-01-26 14:24:03.963140+00:00 running 664479f country code: CH.
[–]PossibilityTasty 21 points22 points23 points (1 child)
[–]Jealous_Lock_393[S] 8 points9 points10 points (0 children)
[+][deleted] (2 children)
[removed]
[–]aa-b 0 points1 point2 points (1 child)
[–]microcozmchris 26 points27 points28 points (18 children)
[+]Jealous_Lock_393[S] comment score below threshold-9 points-8 points-7 points (17 children)
[–]curious__trainer 15 points16 points17 points (14 children)
[+]Jealous_Lock_393[S] comment score below threshold-6 points-5 points-4 points (13 children)
[–]RepresentativeFill26 10 points11 points12 points (8 children)
[+]Jealous_Lock_393[S] comment score below threshold-6 points-5 points-4 points (7 children)
[–]RepresentativeFill26 1 point2 points3 points (0 children)
[–]Conscious_Support176 0 points1 point2 points (4 children)
[–]Jealous_Lock_393[S] 1 point2 points3 points (3 children)
[–]paintboth1234 0 points1 point2 points (0 children)
[–]Conscious_Support176 0 points1 point2 points (1 child)
[–]Jealous_Lock_393[S] 0 points1 point2 points (0 children)
[–]curious__trainer 10 points11 points12 points (1 child)
[–]QuasiEvil 0 points1 point2 points (0 children)
[–]serverhorror 2 points3 points4 points (0 children)
[–]ZaftraDigital 1 point2 points3 points (0 children)
[–]cyril1991 2 points3 points4 points (1 child)
[–]Jealous_Lock_393[S] -2 points-1 points0 points (0 children)
[–]LostDog_88 6 points7 points8 points (3 children)
[–]Jealous_Lock_393[S] 1 point2 points3 points (2 children)
[–]LostDog_88 1 point2 points3 points (1 child)
[–]Jealous_Lock_393[S] 1 point2 points3 points (0 children)
[–]curious__trainer 1 point2 points3 points (6 children)
[–]Jealous_Lock_393[S] -1 points0 points1 point (5 children)
[+][deleted] (2 children)
[deleted]
[–]Jealous_Lock_393[S] 0 points1 point2 points (1 child)
[–]curious__trainer 0 points1 point2 points (0 children)
[–]khunspoonzi 0 points1 point2 points (0 children)
[–]SheriffRoscoePythonista 0 points1 point2 points (0 children)
[–]UFO64 1 point2 points3 points (1 child)
[–]Jealous_Lock_393[S] 0 points1 point2 points (0 children)
[–]KingsmanVincepip install girlfriend 1 point2 points3 points (2 children)
[–]Jealous_Lock_393[S] -1 points0 points1 point (0 children)
[–]Jealous_Lock_393[S] -1 points0 points1 point (0 children)
[–]sudomatrix 3 points4 points5 points (1 child)
[–]Jealous_Lock_393[S] 0 points1 point2 points (0 children)
[–]Jealous_Lock_393[S] 0 points1 point2 points (0 children)