Is there any single function to print an iterable's values?
This rather depends what you want, if you want to print out all the values, you need to compute them - an iterable doesn't guarantee the values are computed until after they are all requested, so the easiest way to achieve this is to make a list:
print(list(iterable))
This will print out the items in the normal list format, which may be suitable. If you want each item on a new line, the best option is, as you mentioned, a simple for loop:
for item in iterable:
print(item)
If you don't need the data in a specific format, but just need it to be readable (not all on one line, for example), you may want to check out the pprint
module.
A final option, which I don't really feel is optimal, but mention for completeness, is possible in 3.x, where the print()
function is very flexible:
print(*iterable, sep="\n")
Here we unpack the iterable as the arguments to print()
and then make the separator a newline (as opposed to the usual space).
You could use the str.join
method and join each element of the iterable on a new line.
print('\n'.join(it))