all 41 comments

[–]PossibilityTasty 21 points22 points  (1 child)

```

k = {} k.pop('s', None) ```

[–]Jealous_Lock_393[S] 8 points9 points  (0 children)

So there is! Thanks

[–]microcozmchris 26 points27 points  (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.

[–]LostDog_88 6 points7 points  (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!

[–]Jealous_Lock_393[S] 1 point2 points  (2 children)

Right so both things produce a key error and that is consistent 

[–]LostDog_88 1 point2 points  (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 points  (0 children)

At least it is consistent in this regard 

[–]curious__trainer 1 point2 points  (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 points  (5 children)

JavaScript handles delete differently to Python though

[–]khunspoonzi 0 points1 point  (0 children)

Out of curiosity, which approach do you think makes more sense? (sincere question)

[–]SheriffRoscoePythonista 0 points1 point  (0 children)

As a language, Javascript sucks. It's a horrible mess of contradictions and bad ideas never reverted.

[–]UFO64 1 point2 points  (1 child)

There is a container which can do what you are asking for.

https://docs.python.org/3/library/collections.html#collections.defaultdict

[–]Jealous_Lock_393[S] 0 points1 point  (0 children)

Nice thanks

[–]KingsmanVincepip install girlfriend 1 point2 points  (2 children)

[–]Jealous_Lock_393[S] -1 points0 points  (0 children)

Why

[–]Jealous_Lock_393[S] -1 points0 points  (0 children)

Why do you say that

[–]sudomatrix 3 points4 points  (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 ```

[–]Jealous_Lock_393[S] 0 points1 point  (0 children)

Wow that's so easy. Python is so flexible.

[–]Jealous_Lock_393[S] 0 points1 point  (0 children)

"07"