> > Assuming dict_sweep worked perfectly it would take input like this: > > A_in = {1: {2: 2, 3: {1: None, 2: 2}}, 2: 2, 3: None} > > B_in = {1: {1: {1: None, 2: {1: None}}, 2: 2, 3: None} > > and output this: > > A_out = {1: {2: 2, 3: {2: 2}}, 2: 2} > > B_out = {2:2} > > This dict_sweep above obviously doesn't work and I'm rather afraid of > hitting python's recursion limit. Does anyone see a way to modify > dict_sweep in it's current state to perform dictionary sweeps like > this? What about a non-recursive implementation of dict_sweep? >
How about this: A_in = {1: {2: 2, 3: {1: None, 2: 2}}, 2: 2, 3: None} def dict_sweep(dictin): for key in dictin.keys(): if dictin.get(key) == None: del dictin[key] elif type(dictin[key]) is dict: dictin[key] = dict_sweep(dictin[key]) return dictin A_out = dict_sweep(A_in) print A_out Running the above returns: {1: {2: 2, 3: {2: 2}}, 2: 2} Regards, John -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- http://mail.python.org/mailman/listinfo/python-list