user13191962
user13191962

Reputation:

how to check if a value exists in a dictionary of dictionaries in python?

Suppose my dictionary of dictionaries called records is like this where first, second etc are keys

 records = {
                 first: {
                    "email": email,
                    "password": password,
                    "pwd_secret" : None
                     }

                 second: {
                    "email": email,
                    "password": password,
                    "pwd_secret" : code
                     }
           }

I then check if code is equal to the value of "pwd_secret" in any of the dicts. The code for that functionality works perfectly however my else statement does not work (if code is not the value of "pwd_secret" in any of the dicts, then i want to raise an error. However currently it just raises an error even if the code exists.) Any suggestions?

 for k, v in records.items():
                pwd_secret = v.get('pwd_secret')
                if pwd_secret == code:
                    hashed_password = hash_password(new_password)
                    v['password'] = hashed_password
                #else:
                    #raise ValueError("code is invalid")

Upvotes: 0

Views: 2722

Answers (3)

RoadRunner
RoadRunner

Reputation: 26315

You can go through the values and use an if condition:

for v in records.values():
    pwd_secret = v.get('pwd_secret')
    if pwd_secret == code:
        # found

You don't really need to go through items because it looks like you don't need the key.

As for your error, ValueError is raised from the else branch because the pwd_secret doesn't equal code. If your not expecting this then you should set a breakpoint inside your editor and step through your code line by line to see what's actually happening.

Another easier debugging step is to just print out what each of these values are:

for v in records.values():
    pwd_secret = v.get('pwd_secret')

    print(f"pwd secret: {pwd_secret} and code: {code}") # print values here

    if pwd_secret == code:
        print("Secret is valid!")
    else:
        raise ValueError("code is invalid")

Which might also point out that v.get('pwd_secret') is giving you the default value None if 'pwd_secret' is not found in the inner dictionaries.

Additionally, if you want to check if any of the inner dictionaries have the code, you can use the built-in function any():

if any(v.get('pwd_secret') == code for v in records.values()):
    print("Secret found!")
else:
    raise ValueError("Secret not found")

Upvotes: 2

DTS
DTS

Reputation: 73

assume this dictionary:

records = {
  'first': {
                "email": 1234,
                "password": 1234,
                "pwd_secret" : None
                 },
             'second': {
                "email": 1234,
                "password": 1234,
                "pwd_secret" : 'code'
                 }
       }

and testing loop:

for v in records.values():
  if v['pwd_secret'] == 'code':
    print('here...')

instead of the print() clause just put the necessary actions you wish to perform. in other words - your code should work, the problem is probably not lying in finding the value in a nested dictionary.

Upvotes: 1

SimfikDuke
SimfikDuke

Reputation: 1148

Function:

def check_value(dict_of_dicts, value):
    return any(value in dict_.values() for dict_ in dict_of_dicts.values()) 

Example:

a = {
      'first': {
         "email": 'email',
         "password": 'password',
         "pwd_secret": None
      },

      'second': {
         "email": 'email',
         "password": 'password',
         "pwd_secret": 'code'
      }
}

check_value(a, 'code')
# True

Upvotes: 1

Related Questions