Combining 2 lists in python
# combine the lists
zipped = zip(alist, blist)
# write to a file (in append mode)
file = open("filename", 'a')
for item in zipped:
file.write("%d, %d\n" % item)
file.close()
The resulting output in the file will be:
1,2
2,3
3,4
5,5
For the sake of completeness, I'll add to Ben's solution that itertools.izip
is preferable especially for larger lists if the result is used iteratively, as the final result is not an actual list but a generator:
from itertools import izip
zipped = izip(alist, blist)
with open("output.txt", "wt") as f:
for item in zipped:
f.write("{0},{1}\n".format(*item))
The documentation for izip
can be found here.