0

Assume python dict:

mydict = {'a': 100, 'b': 200, 'c': 300}

I know one of the values:

value = 200

How to remove the 'b': 200 pair from the dict? I need this:

mydict = {'a': 100, 'c': 300}
Bach
  • 6,145
  • 7
  • 36
  • 61
gwaramadze
  • 4,523
  • 5
  • 30
  • 42
  • You shouldn't create a dictionary called `dict`, it will overwrite the builtin. – Ffisegydd Mar 20 '14 at 11:31
  • You're right, it is just a pseudecode. Renamed it. – gwaramadze Mar 20 '14 at 11:32
  • possible duplicate of [Best way to remove an item from a Python dictionary?](http://stackoverflow.com/questions/5447494/best-way-to-remove-an-item-from-a-python-dictionary) – Aegis Mar 20 '14 at 11:43

4 Answers4

4

Use a dictionary comprehension. Note that (as jonrsharpe has stated) this will create a new dictionary which excludes the key:value pair that you want to remove. If you want to delete it from your original dictionary then please see his answer.

>>> d = {'a': 100, 'b': 200, 'c': 300}
>>> val = 200
# Use d.items() for Python 2.x and d.iteritems() for Python 3.x
>>> d2 = {k:v for k,v in d.items() if v != val}
>>> d2
{'a': 100, 'c': 300}
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • Note that this will build a new dictionary excluding the value, rather than removing it from the original dictionary – jonrsharpe Mar 20 '14 at 11:34
  • OK, this is correct. One followup question: This seems to be iterating over the whole dict. Can't I iterate to a point where the first value is found? – gwaramadze Mar 20 '14 at 11:38
  • If you know that only one key:value pair is in your dictionary then I would suggest jonrsharpe's answer (or similar creating a new dictionary). You can then `break` on the first instance. In any case you'll need to use a proper `for` loop rather than a dict comprehension. – Ffisegydd Mar 20 '14 at 11:41
2

It sounds like you want:

for key, val in list(mydict.items()):
    if val == value:
        del mydict[key]
        break # unless you want to remove multiple occurences
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Is it ok to modify the dict that I currently iterate through? Is this why `mydict.items()` are used? – gwaramadze Mar 20 '14 at 11:47
  • In python3 this solution fails(`RuntimeError: dictionary changed size during iteration`) because mydict.items() return an iterator not a list like python2. – pbacterio Mar 20 '14 at 13:49
  • @pbacterio runs fine for me in 3.3.2 - which version are you using? – jonrsharpe Mar 20 '14 at 13:51
  • Sorry, i'm testing without the `break` statement to remove multiple occurrences. In this case i sugest wrap `mydict.items()` with `list` to prevent this exception. – pbacterio Mar 20 '14 at 13:57
0

You'll need to loop over every items(), either with dict comprehension:

new_dict = {k:v for k,v in my_dict.items() if predicate(value)}

Or modifying the existing dictionary:

for k,v in my_dict.items():
    if not predicate(v):
        del my_dict[k]
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
0

The simplest i found:

for key in [k for k,v in mydict.items() if v==200]:
    del mydict[key]
pbacterio
  • 1,094
  • 6
  • 12
  • i didn't downvote you, but you're unnecessarily creating a list comprehension that you're only iterating through. should use a gen expr instead. i upvoted you back to zero – acushner Mar 20 '14 at 14:28
  • 1
    i don't use a generator because can produce a error if you try to remove multiple occurrences. – pbacterio Mar 20 '14 at 14:32