By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,806 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,806 IT Pros & Developers. It's quick & easy.

Removing duplicates from a dict

P: 1
Hi I have a dictionary that contains data like this

dict = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] , 'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] , 'file3.txt': ['T', 'F' , 'C']}

Could some one please help me write a code that could remove the duplicate values and change the file to

dict = {'file1.txt': ['B' , 'D' ] , 'file2.txt': [ 'G' ] , 'file3.txt': [ 'T' ]}

(only the unique values should remain)
Feb 16 '12 #1
Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
Don't use dict as an identifier. It will mask built-in function dict().

Following are a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
  2.       'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
  3.       'file3.txt': ['T', 'F' , 'C']}
  4.  
  5. # Create a dictionary with a count of labels
  6. dd1 = {}
  7. for seq in dd.values():
  8.     for label in seq:
  9.         v = dd1.get(label, 0)
  10.         dd1[label] = v+1
  11.  
  12. for key in dd:
  13.     dd[key] = [label for label in dd[key] if dd1[label] == 1]
  14. print dd
  15.  
  16.  
  17. dd = {'file1.txt': ['A', 'B' , 'C' , 'D' , 'E' ] ,
  18.       'file2.txt': ['A', 'F' , 'C' , 'G' , 'E' ] ,
  19.       'file3.txt': ['T', 'F' , 'C']}
  20.  
  21. # Create an extended list from dd.values()
  22. extended = dd.values()[0][:]
  23. for seq in dd.values()[1:]:
  24.     extended.extend(seq[:])
  25. for key in dd:
  26.     dd[key] = [label for label in dd[key] if extended.count(label) == 1]
  27. print dd
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> {'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
  2. {'file1.txt': ['B', 'D'], 'file3.txt': ['T'], 'file2.txt': ['G']}
  3. >>> 
Feb 16 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.