[BangPypers] Python List Comprehension Question

Rajiv Subramanian M rajiv.m1991 at gmail.com
Thu Oct 2 20:41:18 CEST 2014

Hi Krace,

I believe as your code returns a generator instance it must be efficient in
handling large input as well.
And as you said in previous reply I've added "not isinstance(item, (str,
bytes))", the code looks like

def flat_it(items):
    for item in items:
        if isinstance(item, Iterable) and not isinstance(item, (str,
            for subitem in flat_it(item):
                yield subitem
            yield item

and it works fine.

But is there any possibilities to use "List Comprehension" methodology ?

def flat_it(items):

    return [item for item in items if not isinstance(item, Iterable) *else

The above solution should be possible, but unfortunately list comprehension
syntax dosen't allows an extra else condition (mentioned in bold letters


  [image: --]
Rajiv Subramanian M
[image: http://]about.me/rajiv.m1991

More information about the BangPypers mailing list