adding type checking for flatten

The current version of flatten will silently fail for a dictionary
or a generator, now flatten checks for list or tuple