0

For example, how could I condense

In [1]: for x in xrange(1,11):
...:     if x%2==0:
...:         print x

into one line?

Edit: Thanks guys! That was exactly what I was looking for. To make this a little more challenging though, is there a way to add elif & else and still have it be on one line?

To use the previous example,

for x in xrange(1,11):
   if x%2==0:
      print x
   else
      print "odd"
David Streid
  • 685
  • 3
  • 12
  • 24
  • There isn't a "unix" programming language. This looks like Python. – cdhowie Jul 28 '14 at 21:44
  • possible duplicate of [One line condition in bash](http://stackoverflow.com/questions/4076561/one-line-condition-in-bash) – Farkie Jul 28 '14 at 21:44
  • "on unix"? as in "in a shell script"? – Marc B Jul 28 '14 at 21:45
  • 2
    Why would you want to? – jonrsharpe Jul 28 '14 at 21:52
  • 2
    `from subprocess import *; open('badcode.c', 'wb').write('#include \nint main(){for (int x=1; x!=11; x++) if (x%2 == 0) printf("%d\n", x);}\n'); subprocess.check_call(['cc', '-o', 'badcode', 'badcode.c'; subprocess.check_call(['./badcode'])` – abarnert Jul 28 '14 at 21:56
  • Obviously in real life you'd want to embed Python so you can, e.g., call `xrange(1,11)` through the C API instead of translating it to the equivalent C. But the point is, you can write code to generate, compile, and run a C program in one line. :P – abarnert Jul 28 '14 at 21:59

4 Answers4

2

For your specific example:

for x in xrange(2, 11, 2): print x

More generally, in terms of whether you can nest blocks on one line, the answer is no. Paraphrasing the documentation on compound statements, a "suite" may not contain nested compound statements if it is in the one-line form. A "suite" is the group of statements controlled by a clause (like a conditional block or the body of a loop).

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • 1
    Yes, in a compound statement, the colon has to be followed by a simple statement (that is, one without a colon of its own) or an indented block. – abarnert Jul 28 '14 at 21:57
  • @abarnert thanks for the confirmation. I have added a little more detail to my answer. – Tom Fenech Jul 28 '14 at 22:07
1

This isn't quite the same and isn't "one line", but consider removing the side-effect and using a list filter/comprehension.

evens = [x for x in xrange(1,11) if x % 2 == 0]
print "\n".join(evens)
# or (now a saner "one line", the evens-expr could even be moved in-place)
for x in evens: print x
abarnert
  • 354,177
  • 51
  • 601
  • 671
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    You've got the `join` backward. In Python, a string joins an iterable, not the other way around. – abarnert Jul 28 '14 at 21:54
  • @abarnert Thanks, that was silly of me - I've corrected the code. – user2864740 Jul 28 '14 at 21:55
  • No, that's still wrong. `str.join` takes the `str` first, then the iterable. But you should call it with as a normal method, not an unbound method, anyway. I'll edit it if you don't mind (you can always undo my edit if you don't like it). – abarnert Jul 28 '14 at 23:48
1

Maybe something like this:

from __future__ import print_function

map(print, [x for x in xrange(1,11) if x % 2 == 0])
Victor Castillo Torres
  • 10,581
  • 7
  • 40
  • 50
  • This works, but it's ugly to use `map` for mapping side effects. Also, in Python 3.x, it won't do anything visible, because `map` just returns a (lazy) iterator, and if you never iterate it, none of those `print` calls get evaluated. – abarnert Jul 28 '14 at 23:48
0
for x in [y for y in xrange(1,11) if y%2==0]:
  print x
nrikee
  • 1
  • 1