views:

176

answers:

8

Say I have a dictionary with whatever number of values. And then I create a list. If any of the values of the list are found in the dictionary, regardless of whether or not it is a key or an index how do I delete the full value?

E.g:

dictionary = {1:3,4:5}
list = [1]
...
    dictionary = {4:5}

How do I do this without creating a new dictionary?

+3  A: 

it's a bit complicated because of your "values" requirement:

>>> dic = {1: 3, 4: 5}
>>> ls = set([1])
>>> dels = []
>>> for k, v in dic.items():
    if k in ls or v in ls:
     dels.append(k)

>>> for i in dels:
    del dic[i]

>>> dic
{4: 5}
SilentGhost
We seem to have interpreted the question in different ways - he was referring to values in the list, and keys/indices in the dictionary. I think he wants to remove all key-value pairs in the dictionary where the key is in the list.
Dominic Rodger
Why create the extra "dels" list? Why not just delete the item directly?
Josh Wright
because I'm iterating over dict, and such deletion would raise a `RuntimeError`.
SilentGhost
@Dominic: there is no such thing as index in dict, the only sensible way I can interpret the question is to assume that OP means keys and values.
SilentGhost
+1  A: 
dictionary = {1:3,4:5}
list = [1]

for key in list:
  if key in dictionary:
     del dictionary[key]
Dominic Rodger
This would only delete entries that have 1 as a key. The question also called for deleting entries where the values matched 1.
twneale
@twneale - in my view, the question is ambiguous - the question asks to delete "keys or indexes" in the dictionary for all values in the list. Some people have interpreted a dictionary index to mean the value, but that doesn't make sense to me.
Dominic Rodger
A: 

I would do something like:

for i in list:
    if dictionary.has_key(i):
         del dictionary[i]

But I am sure there are better ways.

Stefano Borini
Sorry... _delete_.
Stefano Borini
`del` is correct (there's no keyword `delete`!-) but `has_key` is useless (**much** better: `if i in dictionary:`).
Alex Martelli
it was referred to my setting to None. I misunderstood the OP wanted to set it to none.
Stefano Borini
+1  A: 
>>> dictionary = {1:3,4:5}
>>> list = [1]
>>> for x in list:
...     if x in dictionary:
...             del(dictionary[x])
... 
>>> dictionary
{4: 5}
Jaime Soriano
+5  A: 
for key, value in list(dic.items()):
    if key in lst or value in lst:
        del dic[key]

No need to create a separate list or dictionary.

I interpreted "whether or not it is a key or an index" to mean "whether or not it is a key or a value [in the dictionary]"

Josh Wright
I think this breaks in Python 3, where `dict.items()` returns a generator. Best to do `for k, v in list(d.items())` in that case.
Will McCutchen
Good point, updated to reflect your suggestion.
Josh Wright
+1 - though I interpreted the question differently :-)
Dominic Rodger
A: 

A few more testcases to define how I interpret your question:

#!/usr/bin/env python

def test(beforedic,afterdic,removelist):
    d = beforedic
    l = removelist
    for i in l:
        for (k,v) in list(d.items()):
            if k == i or v == i:
                del d[k]

    assert d == afterdic,"d is "+str(d)

test({1:3,4:5},{4:5},[1])
test({1:3,4:5},{4:5},[3])
test({1:3,4:5},{1:3,4:5},[9])
test({1:3,4:5},{4:5},[1,3])
Douglas Leeder
A: 

If the dictionary is small enough, it's easier to just make a new one. Removing all items whose key is in the set s from the dictionary d:

d = dict((k, v) for (k, v) in d.items() if not k in s)

Removing all items whose key or value is in the set s from the dictionary d:

d = dict((k, v) for (k, v) in d.items() if not k in s and not v in s)
Robert Rossney
A: 
def remKeys(dictionary, list):
    for i in list:
        if i in dictionary.keys():
            dictionary.pop(i)
    return dictionary
inspectorG4dget