When i want to export a list in a txt file it displays the elements in one line instead i want each elements in one column. For instance this list:
[1, 2, 3, 4]
I want to export it in a txt file so that my file contains in one column the elements of the list:
1
2
3
4
>Solution :
With one list:
with open('my_file.txt', 'w') as f:
f.write('\n'.join(str(x) for x in my_list))
WIth two lists:
with open('my_file.txt', 'w') as f:
to_write = ""
for i, (a, b) in enumerate(zip(list1, list2)):
to_write += f'{a},{b}\n' if i < len(list1) - 1 else f'{a},{b}'
f.write(to_write)