0
dct={'a':3,'b':1,'c':1,'d':1,'e':2,'f':2}

for key in dct.values():
    #code

I cannot find a way to delete all the keys that have same values, my output should be:

{'a':3}

Basically i only want to have a dictionary with multiple occurrences of values removed, the value is not specifically 1 or 2 it could be any #

Monty
  • 197
  • 2
  • 10
  • Possible duplicate of [Best way to remove an item from a Python dictionary?](http://stackoverflow.com/questions/5447494/best-way-to-remove-an-item-from-a-python-dictionary) – Aravind Nov 22 '16 at 13:35

2 Answers2

2

Try this dict comprehension:

new_dct = {k: v for k, v in dct.items() if list(dct.values()).count(v) <= 1}

This won't delete from your original dictionary, but will generate a new dictionary with only the keys and values where the value is not duplicated in your original dictionary.*


*: Note that in Python 3.x the values() method of dict objects returns a view object, while in Python 2.x it returns a normal list object. Hence the need to call list() on it here in order to use the count() list method, since OP is using Python 3.x. If you are using Python 2.x, you can simply remove the list() call, and call count() on dct.values() directly.

elethan
  • 16,408
  • 8
  • 64
  • 87
0

Easy Solution

dct={'a':3,'b':1,'c':1,'d':1,'e':2,'f':2}
d = {} 
allu = [];
lulu = [];



for  k, v in dct.items():
  if v in allu:
    lulu.append(v)
  allu.append(v)   

d = dict(dct)
for k,v in d.items():
  if v in lulu:
    del dct[k]

print(dct)