0

I would like to concatenate 2 lists with new line.

list1 = [1,2]
list2 = [3,4,5]

The expected output format is:

1,2
3,4,5

My code is:

final = []
final.append(list1)
final.append(list2)

final = '\n'.join([str(i) for i in finallist])

But it returns:

'[1, 2]\n[4, 5, 6]'

Because I am going to save this output to txt file, so it has to be a string and print() does not work here. Could anyone advise how can I solve this problem? Thank you very much!

4
  • When you call str() on an entire list, you get [ ] in the result. It sounds like you want to call str() on the contents of the list, not the list itself. Commented Oct 30, 2020 at 0:34
  • @JohnGordon apparently no. calling str() on a list makes the whole thing a string Commented Oct 30, 2020 at 0:37
  • @Matiiss I'm not sure what you're saying "no" to? Calling str() on a list certainly does include [ ] in the output string... Commented Oct 30, 2020 at 0:42
  • @JohnGordon in that case I misinterpreted your statement of getting [] as I thought it meant an empty list but even if you get [] in the string too you can .replace() them Commented Oct 30, 2020 at 0:44

2 Answers 2

1

does this fix your issue (its your expected output):

list1 = [1,2]
list2 = [3,4,5]

final = [list1, list2]

final = '\n'.join([str(i).replace('[', '').replace(']', '') for i in final])
with open('yourfile.txt', 'w') as file:
    file.write(final)
Sign up to request clarification or add additional context in comments.

3 Comments

Because I am going to save this output to txt file, so it has to be a string and print() does not work here.
@user14504696 I edited my post is this what you wanted?
Yes, this works. Thank you! I found the result showed in Jupyter notebook still has \n while it is okay when saving to txt file.
0

Consider the operations here:

final = []  # An empty list
final.append(list1)  # List contains a list: [[1, 2]]
final.append(list2)  # List contains 2 lists: [[1, 2], [3, 4, 5]]

[str(i) for i in finallist]  # Convert each list into a string, put it in a new list
# e.g. This list will contain two values: ['[1, 2]', '[4, 5, 6]']

It sounds like you want to have commas between values in the list, and newlines between lists. Converting the entire list into a string will include the brackets, when you just want to convert each entry into its own string. Like so:

list1 = [str(i) for i in list1]
list2 = [str(i) for i in list2]
final = [list1, list2]
print('\n'.join([','.join(i) for i in final]))

3 Comments

Because I am going to save this output to txt file, so it has to be a string and print() does not work here.
@user14504696 That's okay; just write '\n'.join([','.join(i) for i in final]) to the output file.
Yes, this works. Thank you! I found the result showed in Jupyter notebook still has \n while it is okay when saving to txt file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.