Python Forum
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionnaries - Equality
#1
Hello, I need your help. I have to write a function which returns True or False according to the equality between a dictionnary and a list.

For example :

l = [[0, 2, 4], [4, 1, 0]]

d = {(0,1): 2, (0,2): 4, (1,0): 4, (1,1): 1}

In this case, function must return True. But when I turn the first key into (0,2) it returns True once again instead of False.

Here is my code :

def equal(l,d) :
    for key in d :
        if d[key] == l[key[0]][key[1]] :
            return True
        else :
            return False
Reply
#2
You should reconsider your logic a little bit. You should look for False and if first one is found you return it (no need for further iteration). If you iterated over all keys and no False was found then you return True (outside the loop).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Here is a modification :

def equal(l,d) :
    n_key = 0
    n_true = 0
    for key in d :
        n_key += 1
        if d[key] == l[key[0]][key[1]] :
            n_true += 1
    if n_key == n_true :
        return True
    else :
        return False
Reply
#4
Before writing any code we should take a time and think about task at hand.

We want to compare for equality.

What is the earliest moment we know that list and dictionary are not equal? When we encounter first mismatch. At that point our code should 'short-circuit' because there is no point in iterating any further as non-equality is already established.

When we know that dictionary and list are equal? Only after we iterated over all keys and code has not 'short circuited'. This means that we know equality only after last item in iteration is checked.

Based on that knowledge we can slightly modificy SupaFlamme initial code:

>>> def equal(l, d):
...     for key in d:
...         if d[key] != l[key[0]][key[1]]:
...             return False
...    return True


As regarding 'But when I turn the first key into (0,2) it returns True once again instead of False.' - keys in dictionaries are unique. If there are two similar keys then last one overwrites previous value (you have two (0,2) keys). If you use Python earlier than 3.7 then dictionaries are not ordered (from 3.7 dictionaries are insertion ordered).

Note of caution: this code checks only dictionary keys. This means that it will return True if all keys match but there are additional elements in list (this means that dictionary and list actually are not equal). To overcome this additional length comparison could be performed (length of dictionary is two times of length of list)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,124 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  Checking for equality PythonGainz 8 3,237 Apr-10-2020, 03:00 PM
Last Post: PythonGainz
  Equality in Python el_bueno 8 3,429 Feb-08-2020, 03:33 PM
Last Post: el_bueno
  Problems with Equality of Numbers SheeppOSU 3 2,378 Jan-04-2019, 07:34 AM
Last Post: Gribouillis
  Shared reference and equality zyo 3 3,152 Jun-30-2018, 07:10 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020