List Comprehension Syntax

Ville Vainio ville at spammers.com
Sun Jul 11 16:22:44 CEST 2004


Moosebumps wrote:
> Just curious -- anyone care to tell me how they would format the above?  (or
> maybe you wouldn't write it all)

Peter indents:
---------------
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
     ]

-----------------

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 :-).

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list