python - More elegant/Pythonic way of printing elements of tuple? -
i have function returns large set of integer values tuple. example:
def solution(): return 1, 2, 3, 4 #etc.
i want elegantly print solution without tuple representation. (i.e. parentheses around numbers).
i tried following 2 pieces of code.
print ' '.join(map(str, solution())) # prints 1 2 3 4 print ', '.join(map(str, solution())) # prints 1, 2, 3, 4
they both work ugly , i'm wondering if there's better way of doing this. there way "unpack" tuple arguments , pass them print
statement in python 2.7.5?
i love this:
print(*solution()) # not valid syntax in python wish
kind of tuple unpacking it's equivalent to:
print sol[0], sol[1], sol[2], sol[3] # etc.
except without ugly indexes. there way that?
i know stupid question because i'm trying rid of parentheses wondering if there missing.
print(*solution())
can be valid on python 2.7, put:
from __future__ import print_function
on top of file.
you iterate through tuple:
for in solution(): print i,
this equivalent to:
for in solution(): print(i, end= ' ')
if ever use python 3 or import statement above.
Comments
Post a Comment