Python Forum
printing a list of lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: printing a list of lines (/thread-4163.html)



printing a list of lines - Skaperen - Jul-27-2017

which is the better way to print a list of lines?

1.
    for line in lines:
       print(line)
2.
    print('\n'.join(lines))
assuming there are not so many lines to eat up your memory.


RE: printing a list of lines - micseydel - Jul-27-2017

(Jul-27-2017, 05:36 AM)Skaperen Wrote: assuming there are not so many lines to eat up your memory.

The second one. It only has to flush I/O one time, as opposed to every line.

You can test this yourself, you should find one significantly faster for larger numbers of lines.


RE: printing a list of lines - Skaperen - Jul-28-2017

i understand that about the 2nd one.  but i found a problem with it that i should have expected to see had i thought more about it.  so i switched back to the first.  the problem happens when the list is empty.


RE: printing a list of lines - buran - Jul-28-2017

well, this problem is easy to solve with
if lines:
    print('\n'.join(lines))
if the gain in speed is worth it


RE: printing a list of lines - micseydel - Jul-28-2017

I suppose it depends on what you're optimizing for "better" then  :)


RE: printing a list of lines - Skaperen - Jul-29-2017

(Jul-28-2017, 03:58 PM)micseydel Wrote: I suppose it depends on what you're optimizing for "better" then  :)

opinions on what way should be used.  i like burans way.


RE: printing a list of lines - DeaD_EyE - Jul-29-2017

(Jul-27-2017, 05:36 AM)Skaperen Wrote: which is the better way to print a list of lines?

1.
 for line in lines:
print(line)
2.
 print('\n'.join(lines))
assuming there are not so many lines to eat up your memory.

I prefer the first option, if there are other things to do with the line like splitting, conversion, filtering etc.
If I don't do anything with the data, just saving it somewhere, I'll prefer the join method.

I think the function is not flushing automatically. Sometimes I had to use flush=True as argument inside the print function, when I was doing some asyncio tasks.
Please forget it to optimize first. They told us over the years that joining strings together is faster as using the operation for addition. This is not always True.
Make it first readable and later you can optimize.

Here an example:
first, second, third = 'Hello ', 'World', '!'

def str_concatination(first, second, third):
    return first + second + third


def str_join(first, second, third):
    return ''.join([first, second, third])
Output:
In [64]: %timeit str_concatination(first, second, third) The slowest run took 8.33 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 3: 145 ns per loop
Output:
%timeit str_join(first, second, third) The slowest run took 8.48 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 3: 195 ns per loop
The time varies from Python version to Python version. Internally there is a heavy optimization. The rule from the last years, that join is faster as the add operation, is not always True. In the IT-World we say: "Proof it" :-)

But please don't tell beginners that join is always faster.


RE: printing a list of lines - Skaperen - Jul-30-2017

yes, i agree, readable is always best.