-3

I am to compare values of dictionary and find if they are congruent like so; If the dict is {'k1':'v2','k2':'v2','k3':'v2'} i want to compare values which are all identical in this case and for python to print Three values are the same The code i am currently using gives me a list of duplicated elements which is no good. CODE:

d1={'k1':'v2','k2':'v2','k3':'v2'}
l=[]
for v in d1.keys():
    for c in d1.keys():
        if d1[v] == d1[c]:
            l.append(c)
if len(l)==0:
    print('No keys have same values!')
else:
    print(l)

Thanks in advance

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    What if you have, say, one `'v1'` and two `'v2'`s? – j1-lee Jan 23 '22 at 04:37
  • Do you want the largest number of keys that share a same value? Or all numbers of keys that share some same value? (e.g. a dict could have 4 keys share one value, while 3 keys share some other value, etc. - do you only want the number for, or do you need the series 4, 3, etc.) And do you require to know what value occurs some number of time, or only the number itself? In general, it helps if you share the exact expected outcome. – Grismar Jan 23 '22 at 04:37
  • if i have the values as v1,v2,v3 it should reply with no keys have same value – Divyansh Jain Jan 23 '22 at 04:49
  • this is a rather simple code i would expect only one key to be similar so the expected outcome for the example code should be 3 keys have same values, i.e. the values for all the keys here is v2,v2,v2 and since there is three keys hence the outcome as 3 keys have same values – Divyansh Jain Jan 23 '22 at 04:51
  • I edited the title of the question to reflect what you are actually asking and the requirements that you clarified. Please see the duplicate link for a full set of answers for how to compare whether all the values are equal. I assume you already know you can use `d1.values()` to get the values, then you can use one of those techniques. – Karl Knechtel Jan 23 '22 at 05:26

3 Answers3

5

If you are looking to see if all of the values of a dictionary are the same, then you could do something like:

d1 = {"k1": "v2", "k2": "v2", "k3": "v2"}
if len(set(d1.values())) == 1:
    print("All values are the same")

Explanation: d1.values() contains all of the values, set() de-duplicates it, and len() provides the length. If there is only 1 de-duplicated value, then all of the values must be the same.

If you are looking specifically for the count of values that are the same (even when they are not all the same, then you could use a variation of this, comparing the number of de-duplicated values to the number of keys.

C.R.
  • 91
  • 1
  • 3
1

This should work -

d1={'k1':'v2','k2':'v2','k3':'v2'}
l={}
for v in d1.keys():
    l[d1[v]] = list(d1.values()).count(d1[v])

if 0 in list(l.values()):
    print('No keys have same values!')
else:
    print(f'{list(l.values())[0]} keys have same values')
sr0812
  • 324
  • 1
  • 9
1

If your values can be keys of a dictionary, in other words is of hashable type, then you can do very fast (linear complexity) algorithm using counting dictionary (and you str type is hashable).

Following code output at the end all duplicates (values that appear more than once), duplicates are shown as pairs of value and number of repetitions.

Try it online!

d1={'k1':'v2','k2':'v2','k3':'v2'}

cnt = {}
for e in d1.values():
    cnt[e] = cnt.get(e, 0) + 1
cnt = sorted(cnt.items(), key = lambda e: e[1], reverse = True)

if len(cnt) > 0 and cnt[0][1] > 1:
    print('There are duplicates:', {a : b for a, b in cnt if b > 1})
else:
    print('No keys have same values!')

Output:

There are duplicates: {'v2': 3}

Same code as above can be implemented shorter using standard library class collections.Counter.

Try it online!

import collections

d1={'k1':'v2','k2':'v2','k3':'v2'}

cnt = collections.Counter(d1.values()).most_common()

if len(cnt) > 0 and cnt[0][1] > 1:
    print('There are duplicates:', {a : b for a, b in cnt if b > 1})
else:
    print('No keys have same values!')

Output:

There are duplicates: {'v2': 3}
Arty
  • 14,883
  • 6
  • 36
  • 69