List Comprehension Syntax

Matthew Scott newsgroups at goldenspud.com
Sun Jul 11 15:32:54 EDT 2004


Ville Vainio wrote:
> Nope, the only correct way is 
> 
> 
> result = [x for x in range(10)
>           if x % 2 == 0
>           if x % 3 == 0
>           ]
> 
> result = [x * y
>           for x in range(10) if x % 2 == 0
>           for y in range(10) if y % 3 == 0
>           ]
> 
> Because that's the way python-mode.el does it in emacs :-).

Unless, of course, you leave the opening bracket as the last character 
on the line, in which case python-mode will prefer you to do this:

result = [
     x for x in range(10)
     if x % 2 == 0
     if x % 3 == 0
     ]

result = [
     x * y
     for x in range(10) if x % 2 == 0
     for y in range(10) if y % 3 == 0
     ]

:)



More information about the Python-list mailing list