removing redundant elements in a dictionary

Astan Chee stanc at al.com.au
Wed Aug 1 05:50:03 CEST 2007


Hi,
I have a dictionary which looks something like this:

elem = 
{"co":[1,2,3],"cp":[3,2,1],"pp":[5,6,7],"cl":[1,2,3],"qw":[6,7,8],"qa":[8,7,6]}

what Im trying to do is find all keys in the list that have the same 
value and delete those (except one); thereby removing all redundant keys 
so that the above dict will look like this:

elem = {"co":[1,2,3],"pp":[5,6,7],"qa":[8,7,6]}

my code so far looks like this:

for out1 in elem.keys():
    for out2 in elem.keys():
        if out1 != out2:
            elem[out1].sort()
            elem[out2].sort()
            if elem[out1] == elem[out2]:
                del elem[out1]

This returns in a KeyError exception when sorting. What am I missing here?
Thanks
Astan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070801/fe0f56d6/attachment.html>


More information about the Python-list mailing list