<html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1250"><META name="Author" content="Novell GroupWise WebAccess"></head><body style='font-family: Tahoma, sans-serif; font-size: 13px; '>Thanks for the help.<br><br>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.<br><br>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.<br><br>d =<br>{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]}<br><br>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:<br><br>new_d=<br>{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 32], 26: [25, 27, 16], 30: [40]}<br><br>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.<br><br>Thanks again,<br>Krishna<br><br><br><br>>>> Dave Angel <davea@ieee.org> 08/11/09 7:38 AM >>><br>Krishna Pacifici wrote:<br>> Hi,<br>> kind of a newbie here, but I have two questions that are probably pretty simple.<br>><br>> 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.<br>> s={}<br>> s[0]=[10,2,3]<br>>  s[10]=[22,23,24]<br>>  s[20]=[45,5]<br>> s[30]=[2,4]<br>> s[40]=[6,7,8]<br>><br>> 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:<br>><br>> s[0]=[3]<br>>  s[10]=[22,23,24]<br>>  s[20]=[45,5]<br>> s[30]=[2,4]<br>> s[40]=[6,7,8]<br>><br>> 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.<br>><br>> 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.<br>><br>> Any suggestions would be greatly appreciated.<br>><br>> Thank you very much,<br>> Krishna<br>><br>><br>>   <br>Sounds like homework.  If it was for an unconstrained project, I'd <br>design a different data structure, one that directly enforced the data <br>constraints.  So far, I can't imagine a useful reason for this <br>particular set of constraints.<br><br>Let's break the problems down.<br><br>1a)   Do you know how to write a loop that visits all the keys of a <br>dictionary?<br>1b)  Do you know how to safely check if a particular key exists?     <br>e.g.      if   key in s:<br>1c)  Do you know how to collect a set of values, so that when a <br>duplicate is found, it can be recognized as such?<br>1d) Do you know how to remove an item from a list?<br><br>2a)  Like 1a)<br>2b) Do you know how to append a value to the end of a list?  Is s[key] a <br>list?<br><br><br>DaveA<br><br></body></html>