dictionary help

Krishna Pacifici pacificik at warnell.uga.edu
Tue Aug 11 17:13:28 CEST 2009

Thanks for the help.

Actually this is part of a much larger project, but I have unfortunately pigeon-holed myself into needing to do these things without a whole lot of flexibility.

To give a specific example I have the following dictionary where I need to remove values that are duplicated with other values and remove values that are duplicates of the keys, but still retain it as a dictionary.  Each value is itself a class with many attributes that I need to call later on in the program, but I cannot have duplicates because it would mess up some estimation part of my model.

d =
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}

So I want a new dictionary that would get rid of the duplicate values of 21, 22, 36 and 20 and give me back a dictionary that looked like this:

{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40]}

I understand that a dictionary may not be the best approach, but like I said I have sort of pigeon-holed myself by the way that I am simulating my data and the estimation model that I am using.  Any suggestions or comments about the above problem would be greatly appreciated.

Thanks again,

>>> Dave Angel <davea at ieee.org> 08/11/09 7:38 AM >>>
Krishna Pacifici wrote:
> Hi,
> kind of a newbie here, but I have two questions that are probably pretty simple.
> 1.  I need to get rid of duplicate values that are associated with different keys in a dictionary.  For example I have the following code.
> s={}
> s[0]=[10,2,3]
>  s[10]=[22,23,24]
>  s[20]=[45,5]
> s[30]=[2,4]
> s[40]=[6,7,8]
> Now I want to be able to loop through the primary keys and get rid of duplicates (both in keys and values) so that I would have either a new dictionary or the same dictionary but with the following values:
> s[0]=[3]
>  s[10]=[22,23,24]
>  s[20]=[45,5]
> s[30]=[2,4]
> s[40]=[6,7,8]
> It doesn't matter which value gets removed as long as there is only one remaining, so in this example it doesn't matter that 2 got removed from s[0] or from s[30] as long as there is only one 2 in the dictionary.
> 2.  I need to be able to loop over the values in the dictionary when there are multiple values assigned to each key like above and assign new values to those values.  Taking the above example I would want to assign a new value so that when you called s[0] it would equal [3,4] say if 4 was the new value.  I think this should be as simple as adding a value, but I kept on having difficulty.
> Any suggestions would be greatly appreciated.
> Thank you very much,
> Krishna
Sounds like homework.  If it was for an unconstrained project, I'd 
design a different data structure, one that directly enforced the data 
constraints.  So far, I can't imagine a useful reason for this 
particular set of constraints.

Let's break the problems down.

1a)   Do you know how to write a loop that visits all the keys of a 
1b)  Do you know how to safely check if a particular key exists?     
e.g.      if   key in s:
1c)  Do you know how to collect a set of values, so that when a 
duplicate is found, it can be recognized as such?
1d) Do you know how to remove an item from a list?

2a)  Like 1a)
2b) Do you know how to append a value to the end of a list?  Is s[key] a 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090811/eb19bf03/attachment.html>

More information about the Python-list mailing list