Outputting space-separated numbers

Add a reply

Posted by mick 662 days ago:
Several of these challenges involve outputting space-separated lists of numbers. However, for us Python codegolfers, that's a bit unfortunate, since Python prints with commas and brackets:

>>> x=[1,2,3,4]
>>> print x
[1, 2, 3, 4]

So I've been outputting space-separated numbers like this:

>>> print" ".join(map(str,x))
1 2 3 4

But at a cost of 18-keystrokes more than print x. :-(

I just figured out that:

>>> for i in x:print i,

Only costs 12-keystrokes, but it's missing the '\n' at the end. If you need it, then:

>>> for i in x+['\n']:print i,

means that the extra seven strokes for "+['\n']" makes it worse than join/map/str.

Anyone else find themselves wishing the required output was more like [1, 2, 3, 4]? Or at least 1, 2, 3, 4, so that we could do:

>>> print str(x)[1:-1]

to slice off the brackets? (Cost: 11 keystrokes more than 'print x')

Anyone got an improvement over these methods that they'd like to suggest?
Posted by pbx 662 days ago:
The only hack that I don't see you mentioning is backticks. They can be useful when working with numerical input but building up an output list of strings.

Useless example:

>>> r = range(10)
>>> r
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> ''.join(`n`for n in r)
'0123456789'
>>> ''.join(map(str,r))
'0123456789'
Posted by cdr 661 days ago:
Would this work?

print(len(x)*'%i ')%tuple(x)

It isn't actually any better unless you know in advance how long your list is - if you could replace len(a) with a fixed number (and the number is one or two digits), it would be shorter than the join one.

(From some posting on a python mailing list...)
Posted by lifthrasiir 654 days ago:
Anyone else find themselves wishing the required output was more like [1, 2, 3, 4]? Or at least 1, 2, 3, 4, so that we could do:

>>> print str(x)[1:-1]

to slice off the brackets? (Cost: 11 keystrokes more than 'print x')

For list str(x) is equivalent to repr(x), which is same to `x`.

Would this work?

print(len(x)*'%i ')%tuple(x)

It works, and actually the following is shorter:

print'%i '*len(x)%tuple(x)

...because % and * have same precedences.
Posted by mick 653 days ago:
Would this work?

print(len(x)*'%i ')%tuple(x)

It works, and actually the following is shorter:

print'%i '*len(x)%tuple(x)

...because % and * have same precedences.

Unfortunately, neither of these work for many of the challenges, since it generates an excess space at the end. :-( And unless you can generate x as a tuple, they're both longer than:

print" ".join(map(str,x))

Follow-up question on this topic: Is this the shortest way to read in a space-separated list of numbers?

x=map(int,raw_input().split())
Posted by Brian 469 days ago:
Another way when you don't want spaces is to use:

>>> r=range(10)
>>> print`r`[1::3]

This saves 5 bytes over ''.join(map(str,r)), but you can only use it for single-character items. It can also be a slight saving when joining lists of characters vs ''.join(x) (using `x`[2::5]), so long as you don't have backslashes or non-printable characters involved.

Add a reply