Assume there're three lists:
list1 = [a1, a2, a3]
list2 = [b1, b2, b3]
list3 = [c1, c2, c3]
how could I print lists one after another to get following result:
a1, b1, c1
a2, b2, c2
a3, b3, c3
Assume there're three lists:
list1 = [a1, a2, a3]
list2 = [b1, b2, b3]
list3 = [c1, c2, c3]
how could I print lists one after another to get following result:
a1, b1, c1
a2, b2, c2
a3, b3, c3
You could use zip
to combine the lists element-wise, then join
to create a string delimited by commas from each row.
list1 = ['a1', 'a2', 'a3']
list2 = ['b1', 'b2', 'b3']
list3 = ['c1', 'c2', 'c3']
for row in zip(list1, list2, list3):
print(', '.join(row))
Output
a1, b1, c1
a2, b2, c2
a3, b3, c3
list1 = ['a1', 'a2', 'a3']
list2 = ['b1', 'b2', 'b3']
list3 = ['c1', 'c2', 'c3']
for row in zip(list1, list2, list3):
print(", ".join(row))
a1, b1, c1
a2, b2, c2
a3, b3, c3
zip()
returns an iterator in Python 3. You could use itertools.izip()
on Python 2 as @Ashwini Chaudhary suggested.
You could simply use
for i in range(0,len(list1)):
print list1[i],",",list2[i],",",list3[i]