如何展平 python 中的列表列表

How to flatten a list of lists of lists in python

我看到了几个关于如何展平表单列表的答案

[1,[1,2],[3]]    
print list(itertools.chain(*[1,[1,2],[3]]))  

但是如何像这样展平列表:

[[1],[[1,2],[3]]]

print list(itertools.chain(*[[1],[[1,2],[3]]]))
[1, [1, 2], [3]]

我经常用这个recipe:

import collections


def flatten(l):

    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, str):
            for sub in flatten(el):
                yield sub
        else:
            yield el


print(list(flatten([[1],[[1,2],[3]]])))
# [1, 1, 2, 3]