1
def uniq(x, k):
   for key in x:
       if (x.get(key) == x.get(k)):
           if (x.get(key) == x.get(k)):
              return False
   return True

# Testing
d1 = {'a': 1, 'b': 2, 'c': 2, 'd': 4}
k = 'a'
x = uniq(d1, k)
print(x)

I am wondering how can I check an if statement twice. I need to figure out at parameter key k if there is a duplicate value in the dictionary. The output of this function should be True at key 'a' and at 'c' it should be False.

0

1 Answer 1

1

to check if you have a duplicate value in your dict you can use collections.Counter:

from collections import Counter

def uniq(x, k):
    count = Counter(x.values())
    return count[x[k]] == 1

# Testing
d1 = {'a': 1, 'b': 2, 'c': 2, 'd': 4}
k = 'a'
x = uniq(d1, k)
print(x)
# True

you are counting the number of appearances of every value and then check if the wanted value appears more than 1 time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.