Is there a shorter way of taking a dict with varying length lists and printing it to screen? Shortcomings of this method also include that values which aren't lists are printed as letters separated by spaces, and values which are numbers produce an error. Would it be better to write a class which could accept any type of dict value, or is it best to have different classes for different types of dict?
class PrintDict(object):
"""prints dict keys and values including lists
except if values are numbers"""
def print_dict_to_screen(self, data):
self.data = data
print ""
for key in sorted(self.data):
print "\n\n%s" % str(key)
items = len(self.data[key])
iterate = -1
for i in range(items):
iterate += 1
print self.data[key][iterate],
crops = {'trees' : ['hazelnut'],
'potatoes': ['early', 'main'],
'legumes': ['broad beans', 'peas', 'runner beans'],
'brassicas': ['cabbages', 'sprouts', 'kale', 'swede', 'turnip'],
'onions and roots': ['carrots', 'parsnips',
'beetroot', 'onions', 'shallots'],
'other': ['lettuce', 'sweet corn', 'flowers'],
'fruit': ['blackberry', 'blackcurrant', 'raspberry']}
print_ = PrintDict()
print_.print_dict_to_screen(crops)