Flatten an iterable recursively
iterable must be iterable. If exceptions is specified, any element which is an instance of a type in exceptions (in exactly the built-in isinstance() sense), will not be flattened even if it is iterable.
Note that the default, exceptions=basestring, means that strings will not be flattened into their constituent characters, but dicts will be flattened into their keys. Use exceptions=() to cause all iterable items to be recursively flattened.
Returns a list that contains all exception and non-iterable elements found in the iterable and all its recursively contained iterables.
>>> test_item = [[[1,2,3], (42,None)], [4,5], [6], 7, 'foo', {'foo':'bar', 10:'baz'}]
>>> flatten_iterable(test_item)
[1, 2, 3, 42, None, 4, 5, 6, 7, 'foo', 10, 'foo']
>>> flatten_iterable(test_item, exceptions=())
[1, 2, 3, 42, None, 4, 5, 6, 7, 'f', 'o', 'o', 10, 'f', 'o', 'o']
>>> flatten_iterable(test_item, exceptions=(dict,basestring))
[1, 2, 3, 42, None, 4, 5, 6, 7, 'foo', {10: 'baz', 'foo': 'bar'}]
>>> nested_tuples = ((),((),))
>>> flatten_iterable(nested_tuples, exceptions=tuple)
[(), ((),)]
>>> flatten_iterable(nested_tuples, exceptions=())
[]
>>> flatten_iterable(23)
Traceback (most recent call last):
...
TypeError: 'int' object is not iterable