Print all values ​​from multiple lists at once

Suppose I have 3 lists like these

l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]

How can I print all of these lists at once? What is the pythonic way to do something like this?

for f in l1,l2 and l3:
    print f 

This seems to allow for 2 lists.

Desired conclusion: for each element in all lists I print them using another function

def print_row(filename, status, Binary_Type):
    print " %-45s %-15s %25s " % (filename, status, Binary_Type)

and I call the above function inside the for loop.

+5
source share
7 answers

I think you might need zip:

for x,y,z in zip(l1,l2,l3):
    print x,y,z  #1 4 7
                 #2 5 8
                 #3 6 9

What are you doing:

for f in l1,l2 and l3:

a little strange. This is basically equivalent for f in (l1,l3):, since l2 and l3returns l3(assuming l2and l3two non-empty - otherwise it returns a null).

, :

for lst in (l1,l2,l3):  #parenthesis unnecessary, but I like them...
    print lst   #[ 1, 2, 3 ]
                #[ 4, 5, 6 ]
                #[ 7, 8, 9 ]
+10

zip, +. l1 + l2 + l3 , l1, l2 l3, :

for f in l1+l2+l3:
    print(f)

and . , , - (, l1, l2, l3) , , 3 . , l1, l2, l3, ( ), , .

+3

1 4 7
2 5 8
3 6 9

:

for i,j,k in zip(l1,l2,l3):
    print i,j,k
+2

, ,

>>> #Given
>>> l1,l2,l3 = [1,2,3],[4,5,6],[7,8,9]
>>> #To print row wise
>>> import itertools
>>> for f in itertools.chain(l1,l2,l3):
    print(f,end=" ")


1 2 3 4 5 6 7 8 9 
>>> #To print column wise
>>> for f in itertools.izip(l1,l2,l3):
    print(*f,end=" ")


1 4 7 2 5 8 3 6 9 
>>> 

, Python 2.7

>>> for f in itertools.chain(*itertools.izip(l1,l2,l3)):
    print f,


1 4 7 2 5 8 3 6 9 
>>> 
+2

, , map:

>>> l1 = [1, 2, 3]
>>> l2 = [4, 5, 6]
>>> l3 = [7, 8, 9, 2]
>>> for x, y, z in map( None, l1, l2, l3):
...     print x, y, z
...
1 4 7
2 5 8
3 6 9
None None 2
+1

Abhijit, itertools .

>>> [ n for n in itertools.chain(l1, l2, l3) ]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
+1

, 3 , 3 , zip() print() :

[ print(row) for row in zip(l1, l2, l3) ]

(). :

[ print("{} / {} / {}".format(*row)) for row in zip(l1, l2, l3) ]

, .

0
source

All Articles