-5

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
os1
  • 3
  • 4

3 Answers3

4

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
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1
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

zip() returns an iterator in Python 3. You could use itertools.izip() on Python 2 as @Ashwini Chaudhary suggested.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

You could simply use

for i in range(0,len(list1)):
   print list1[i],",",list2[i],",",list3[i]
nEO
  • 5,305
  • 3
  • 21
  • 25
  • if you got what you were looking for, please accept this as an answer. – nEO Oct 14 '14 at 18:53
  • I made a correction. You should is OK. But I should use zip in python since it returns a iterator. – os1 Oct 14 '14 at 19:04