I have a list which has items that are either dicts or something else.
I want to write a method which prints the list, each item on a line, but special-cases the dicts; something like:
def printSpecialList(mylist):
for item in mylist:
if itemIsDict(item):
printDictItem(item)
else:
print str(item)
I can implement everything but itemIsDict — what’s the simplest way to do that?
just a clarification:
The source of this list produces items using dictionary literals e.g. {'a': 3, 'b': 4}, which makes it cumbersome to use special dictionary types that know how to format themselves. In addition, my printSpecialList method is a little more complicated, and it has its own private state that it incorporates, so instance testing, albeit “yucky” (e.g. someone couldn’t make a mock dict class that doesn’t descend from dict that would work with my method) seems like the best way to go here.
1 Answer