print names of dictionaries

Thomas Nelson thn at mail.utexas.edu
Wed Apr 26 21:53:37 EDT 2006


I meant something like
def printdict(dictionaries=[(apps,'apps'), (dirs,'dirs'),
(sites,'sites')]):
       for dictionary,name in dictionaries:
              print name
              keys = dictionary.keys()
              keys.sort()
              for key in keys:
                     print key, ":",dictionary[key]
              print '\n',



It's not really easier than what you did.  Instead of making sure every
dictionary in the dictionaries argument contains a 'name' key that does
what you expect, you must make sure the argument passed in is a list of
(dictionary, name) pairs.  It's a little better in my personal opinion,
because you don't have to modify the dictionaries themselves, and it
avoids the problem of 'name' already existing in the dictionary, as
described by Scott Daniels.  But I suppose that's only one opinion.

THN




More information about the Python-list mailing list