Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing a list of lines
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(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.
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
well, this problem is easy to solve with
if lines:
    print('\n'.join(lines))
if the gain in speed is worth it
Reply
#5
I suppose it depends on what you're optimizing for "better" then  :)
Reply
#6
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
yes, i agree, readable is always best.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python-docx: preserve formatting when printing lines Tmagpy 4 2,104 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
Question Printing through list.. again jesse68 2 1,147 Apr-16-2022, 03:24 PM
Last Post: jesse68
  help for list printing jip31 8 3,638 May-01-2021, 03:52 AM
Last Post: Pedroski55
  Problem printing last element from a list tester_V 3 2,408 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,124 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,839 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Printing images from a list Heyjoe 4 2,830 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  how do i get rid of next lines in my list() ironpotatoe58 10 4,023 Mar-31-2020, 03:57 AM
Last Post: SheeppOSU
  printing a list contents without brackets in a print statement paracelx 1 2,134 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Random nr. no repetition & printing multiple lines Joey 7 2,796 Feb-05-2020, 04:23 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020