There are 3 types of entries in a nested defaultdict a
of structure a=defaultdict(lambda: defaultdict(list))
.
for i in a:
print a[i]
defaultdict(<type 'list'>, {'ldap_uidnumber': [['10002']], 'file': ['ABC', 'xyz']})
defaultdict(<type 'list'>, {'ldap_uidnumber': [], 'file': ['abcd']})
defaultdict(<type 'list'>, {'file': ['/home/testuser/.ssh/id_rsa.pub']})
ldap_uidnumber: []
?ldap_uidnumber
at all?The code I tried:
for i in a:
if a[i]["ldap_uidnumber"] and a[i]["ldap_uidnumber"] == []:
print i
But that is not printing anything, but creating the key in the third value after this code, and looks like
defaultdict(<type 'list'>, {'ldap_uidnumber': [], 'file': ['/home/testuser/.ssh/id_rsa.pub']})
Checking for membership using in
doesn't create the key in a defaultdict. I would recommend that.
for i in a:
if "ldap_uidnumber" in a[i] and # shortcircuit here in your 3rd el
not a[i]['ldap_uidnumber']: # fail here in your 1st el
# do something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With