I am trying to write alternating elements of multiple lists to a file using Python. I can write all of one list, and then all of another list, but am having difficulty doing it in an alternate order. My code looks like this:
foo = ['a', 'b', 'c']
bar = ['1', '2', '3']
fileout = open('zut.txt', 'w')
for i, el in foo, bar:
fileout.write('%s\t%s' % (i, el))
However this produces a ValueError when I try and run it. For clarification, I am trying to produce a file like this:
a 1
b 2
c 3
Can anyone help me to achieve this? Thanks!
You can use the following code:
Read more about ‘zip’ and use ‘with open…’ to be sure that file will be automatically closed