Search code examples
pythonpep8

List comprehension and or spacing with a new-line


This seems like a really simple question; but I can't see how it's actually possible. I normally fairly good my code being PEP8 compliant. 83 characters is fine type of thing. I've got a longish list (dictionary) comprehension combined with an or that I'm trying to take to a new-line but I can't work out how to get the or onto the new-line.

A much simplified version is:

>>> test = {'a' : None, 'b' : None}
>>> b = ','.join([k for k in test
...               if test[k]]) or 'hello'

Whenever ( wherever ) I try to put the or 'hello' on a new-line it fails miserably; the command line interpreter and emacs' parser don't understand either so it may not be possible.

Is it possible to put or 'hello' on a new line and if so where would it go?


Solution

  • Enclose in parentheses. This will work.

    >>> test = {'a' : None, 'b' : None}
    >>> b = (','.join([k for k in test if test[k]])
    ...      or 'hello')