1

I have tried ways on converting list to strings as found in other questions but the output doesn't seem to be what I should get. I have an input of [['This','is','here.'],['Second','sentence','here.']] and I would like to convert this to string and be saved to a text file with the output:

This is here.
Second sentence here.

what I did is,

list = [['This','is','here.'],['Second','sentence','here.']]
with open(outfile, 'w') as newfile:
    newfile.write(str('\n'.join((str(i) for i in list))))

The output of this in the .txt file is:

['This', 'is', 'here.']
['Second', 'sentence', 'here.']

A help is very much appreciated. thanks!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
athrun
  • 29
  • 9

2 Answers2

1

You can try lst = '\n'.join(' '.join(i) for i in txt) (Also, it is good to use variable name other than list):

txt = [['This','is','here.'],['Second','sentence','here.']]
with open(outfile, 'w') as newfile:
    lst = '\n'.join(' '.join(i) for i in txt)
    newfile.write(lst)

Update:

Looks like the the item in list has None, you can filter them as explained in other answer and use following:

txt = [['This','is','here.'],['Second','sentence','here.'], ['Here',None, 'is']]
lst = '\n'.join(' '.join(list(filter(None.__ne__, i))) for i in txt)
print(lst)

Result:

This is here.
Second sentence here.
Here is

As suggested in comments below, just adding additional timing results for comparing filter to list comprehension in this case, and list comprehension is faster:

%%timeit
txt = [['This','is','here.'],['Second','sentence','here.'], ['Here',None, 'is']]
lst = '\n'.join(' '.join(list(filter(None.__ne__, i))) for i in txt)

# Result: 100000 loops, best of 3: 4.52 µs per loop

%%timeit
txt = [['This','is','here.'],['Second','sentence','here.'], ['Here',None, 'is']]
lst = '\n'.join(' '.join(([j for j in i if j != None])) for i in txt)

# Result: 100000 loops, best of 3: 3.28 µs per loop
niraj
  • 17,498
  • 4
  • 33
  • 48
  • Side question... Let's say I wanted to give a right/left pad between the words, how can I achieve it? – trinaldi Jun 16 '18 at 23:44
  • I am not quite sure I understand the question, what is the expected output in above text? – niraj Jun 16 '18 at 23:49
  • I had the error when I tried it: TypeError: sequence item 0: expected str instance, NoneType found @student – athrun Jun 16 '18 at 23:53
  • @student the expected output are two sentences where the second sentence is on the next line. – athrun Jun 16 '18 at 23:55
  • @student I'm not OP, sorry about that. I just have the same list comprehension but I want my output to be like a table (hence the padding) – trinaldi Jun 17 '18 at 00:00
  • 1
    @student yeah you're right there is None in my actual list. sorry about the sample text. It worked! Thanks for the help! – athrun Jun 17 '18 at 00:12
  • `Happy Coding`. @Tico I am not sure but if you have question posted I can try to help. – niraj Jun 17 '18 at 00:13
  • @student Nice -- the `filter(None.__ne__, i)` part is really interesting, are there any advantages / differences to just doing e.g. a list comprehension with `if i`? Thanks! – patrick Jun 17 '18 at 01:30
  • 1
    @patrick Thanks for the comment. I added the results from timing, `list comprehension` is better as usual I think. – niraj Jun 17 '18 at 01:39
  • @Tico If I understand your question correctly, [string formatting](https://pyformat.info/) might be what you are looking for – patrick Jun 17 '18 at 01:49
  • 1
    @student Yeah, but I was having trouble placing the function at the right place. I end up adding a `ljust()` while `push`ing the items to my list – trinaldi Jun 17 '18 at 18:06
  • @Tico may be you can create `dataframe` using `pandas` and print it as table? – niraj Jun 19 '18 at 02:36
  • 1
    @student it's just a small project for some friends, but that's a good idea – trinaldi Jun 21 '18 at 01:00
0

You can use str.join to convert list to strings

>>> l=[['This','is','here.'],['Second','sentence','here.']]
>>> print ('\n'.join([' '.join(e) for e in l]))
This is here.
Second sentence here.
>>> 
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • i tried this but it resulted to an error: TypeError: sequence item 0: expected str instance, NoneType found – athrun Jun 16 '18 at 23:50