Brzozova
Brzozova

Reputation: 382

Can't compare variables from two dictionaries in Python

I have two dictionaries and I want to compare their values srvuuid in hosts dictionary and srvuuidorg in backup dictionary.

This is hosts:

hosts = {}
for d in data['servers']['server']:
    srvhostname = d['hostname']
    srvuuid = d['uuid']
    hosts[srvhostname] = srvuuid

This is backup:

backup = {}
for u in backup_list['storages']['storage']:
    srvuuidorg = u['origin']
    backup_status = u['state']
    backup[srvuuidorg] = backup_status

I extract machine hostname, then check if this hostname exist in hosts dictionary and get the output with srvuuid

hostname = gethostname()
print(hostname)
for srvhostname in hosts:
  if srvhostname != hostname:
    continue
  if srvhostname == hostname:
    print(srvhostname + " : " + hosts[srvhostname])
    break
  else:
    print("There is no matching hostname or uuid.")

At the end I want to compare srvuuid and srvuuidorg. The code below is not working which means there is no output when executing:

for srvuuidorg in backup:
  if srvuuidorg != srvuuid:
      continue
  if srvuuidorg == srvuuid:
      print("Status for " + srvuuidorg + " is " + backup_status)
      break
  else:
      print("No maching uuid found.")

How should I compare two values from this dictionaries or maybe I should change approach to the subject and use something different than comparing dictionaries?

Upvotes: 0

Views: 247

Answers (2)

jon
jon

Reputation: 31

I think what you might want is:

srvuuid_match = False

for srvuuidorg in backup:
    if srvuuidorg == srvuuid:
        print("Status for " + srvuuidorg + " is " + backup[srvuuidorg])
        srvuuid_match = True
        break

if srvuuid_match is False:
    print("No maching uuid found.")

This will print the value stored on the srvuuidorg key of the backup dictionary. I think the problem you were having is that you were using the old value of backup_status from your previous chunk of code, rather than the right value of backup_status stored on the backup[srvuuidorg] dict and key.

However, it's not necessary to loop. You could do:

if srvuuid in backup:
    print("Status for " + srvuuidorg + " is " + backup[srvuuid])
else:
    print("No maching uuid found.")

If srvuuidorg == srvuuid (i.e. the srvuuid key exists in the backup dict), this will print the value stored on the srvuuid key in the backup dictionary. That might be a better way of doing it. Hope that helps!

Upvotes: 1

gstukelj
gstukelj

Reputation: 2551

You don't need to loop over a dict to see if key is present, lookup time is O(1):

hostname = gethostname()
print(hostname)

if hostname in hosts:
    print(srvhostname + " : " + hosts[hostname])
    #srvuuid = hosts[hostname]                      ## I'm just guessing here 
else:
    print("There is no matching hostname or uuid.")

if srvuuid in backup:
    print("Status for " + backup[srvuuid] + " is " + backup_status)
else:
    print("No maching uuid found.")

Upvotes: 0

Related Questions