0

I want to return true if all the values in dict for different keys is same else false:

for example:

this should return true:

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]}) 

this should return false.

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}) 

I tried the following but it didnt work.

        # for i in range(len(votes_grid[0])):
        #     isTie = any(v == i for v in columnTable.values())
        #also tried
              isTie = all(v == i for v in columnTable.values())
2
  • Please don't add a new question to an existing question. Instead, you should ask a new question with the relevant example, what you have tried, and desired result...
    – dawg
    Commented Feb 5, 2021 at 2:53
  • Do you want this to return true or false? {1: [1, 1, 1], 2: [2, 2, 2]}
    – D Hudson
    Commented Feb 9, 2021 at 9:51

5 Answers 5

4

Same as @sarartur but more efficient, because I'm not nesting .values() inside itself. That makes this O(n) rather than O(n**2)

pattern = list(columnTable.values())[0]
tie = all(value == pattern for value in columnTable.values())
2

Try:

tie = all(value == list(columnTable.values())[0] for value in columnTable.values())
2

Although it was a bit derived from Mahdi's answer, this would work in one line:

dic = {1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]} 
check  = len(set(tuple(x) for x in dic.values())) == 1

You cannot put a list of lists in an set because Lists are not hashable objects. You can however make them a list of tuples, which are hashable and thus can be put into a set :)

0
2

Given:

di1={1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]} 
di2={1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}
di3={1: [1, 2, 1], 2: [1, 2, 1], 3: [1, 2, 1]}
di4={1: [1, 1, 2], 2: [1, 2, 1], 3: [1, 2, 1]} # same but different order

If the order of the items in the lists does not matter, you can make a set of frozensets that will show if they are all the same values, regardless of order:

for d in (di1,di2,di3, di4):
    print(d)
    print(len({frozenset(e) for e in d.values()})==1)

Prints:

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]}
True
{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}
False
{1: [1, 2, 1], 2: [1, 2, 1], 3: [1, 2, 1]}
True
{1: [1, 1, 2], 2: [1, 2, 1], 3: [1, 2, 1]}
True

If the order of the lists is relevant (ie, di4 should be False), you can use a set of tuples instead of a set of frozensets:

for d in (di1,di2,di3, di4):
    print(d)
    print(len({tuple(e) for e in d.values()})==1)

Prints:

{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]}
True
{1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}
False
{1: [1, 2, 1], 2: [1, 2, 1], 3: [1, 2, 1]}
True
{1: [1, 1, 2], 2: [1, 2, 1], 3: [1, 2, 1]}
False
0
0

You could convert the values to a set and check that it only has one item:

d1 = {1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 1, 1]}
d2 = {1: [1, 1, 1], 2: [1, 1, 1], 3: [1, 2, 1]}

print(len(set(map(tuple,d1.values())))==1) # True    
print(len(set(map(tuple,d2.values())))==1) # False

You can also do it using accumulate for the comparisons (provided that not all lists are empty):

from itertools import accumulate
print(all(accumulate(d1.values(),lambda a,b:a*(a==b)))) # True
print(all(accumulate(d2.values(),lambda a,b:a*(a==b)))) # False

Not the answer you're looking for? Browse other questions tagged or ask your own question.