Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing a list of lines
#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


Messages In This Thread
printing a list of lines - by Skaperen - Jul-27-2017, 05:36 AM
RE: printing a list of lines - by micseydel - Jul-27-2017, 04:16 PM
RE: printing a list of lines - by Skaperen - Jul-28-2017, 03:26 AM
RE: printing a list of lines - by buran - Jul-28-2017, 07:04 AM
RE: printing a list of lines - by micseydel - Jul-28-2017, 03:58 PM
RE: printing a list of lines - by Skaperen - Jul-29-2017, 03:20 AM
RE: printing a list of lines - by DeaD_EyE - Jul-29-2017, 10:32 AM
RE: printing a list of lines - by Skaperen - Jul-30-2017, 03:28 AM

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