Delete dict and subdict items of some name

Terry Reedy tjreedy at udel.edu
Tue Dec 18 17:29:37 CET 2012


On 12/18/2012 10:27 AM, Gnarlodious wrote:
> On Tuesday, December 18, 2012 3:31:41 AM UTC-7, Hans Mulder wrote:
>> On 18/12/12 06:30:48, Gnarlodious wrote:

>>> from plistlib import readPlist

I do not see this used below.

>>> def explicate(listDicts):
>>> 	for dict in listDicts:
>>> 		if 'FavIcon' in dict:
>>> 			del dict['FavIcon']
>>> 		if 'Children' in dict:
>>> 			dict['Children']=explicate(dict['Children'])
>>> 	return listDicts

>> It would be more Pythonic to return None, to indicate that you've
>> changed the list in situ.

And since it is being changed at the top level (by deletion), it should 
be changed in place all the way down.

>> Since None is the default return value, this means you can leave
>> out the return statement.

  			dict['Children']=explicate(dict['Children'])
would then need to be
  			explicate(dict['Children'])

> But then it only operates on the outer layer,
 > inner layers might get processed but not written.

I believe the above answers your concern. But to be sure it is correct, 
YOU NEED TEST CASES. In fact, your original post should have contained 
at least one non-trivial test case: an input dict and what you wanted it 
to look like after processing. Writing at least some tests before code 
is a great idea.

-- 
Terry Jan Reedy




More information about the Python-list mailing list