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!
str()on an entire list, you get[ ]in the result. It sounds like you want to callstr()on the contents of the list, not the list itself.str()on a list makes the whole thing a stringstr()on a list certainly does include[ ]in the output string...[]as I thought it meant an empty list but even if you get[]in the string too you can.replace()them