Suppose I have 3 lists such as these
l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]
how do I get to print out everything from these lists at the same time ? What's the pythonic way to do something like that ?
for f in l1,l2 and l3:
print f
This only seems to be taking 2 lists into account.
Desired output: for each element in all the lists, I'm printing them out using a different 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.
asked Aug 20, 2012 at 15:52
cyberbemoncyberbemon
2,88011 gold badges36 silver badges61 bronze badges
5
I think you might want zip
:
for x,y,z in zip[l1,l2,l3]:
print x,y,z #1 4 7
#2 5 8
#3 6 9
What you're doing:
for f in l1,l2 and l3:
is a little strange. It is basically equivalent to for f in [l1,l3]:
since l2 and l3
returns l3
[assuming that l2
and l3
are both non-empty -- Otherwise, it will return the empty one.]
If you just want to print each list consecutively, you can do:
for lst in [l1,l2,l3]: #parenthesis unnecessary, but I like them...
print lst #[ 1, 2, 3 ]
#[ 4, 5, 6 ]
#[ 7, 8, 9 ]
answered Aug 20, 2012 at 15:54
mgilsonmgilson
289k60 gold badges603 silver badges675 bronze badges
3
No need to use zip
, just add them together using the +
operator. l1 + l2 + l3
creates a new list that is the combination of l1
, l2
and l3
so you can
simply loop through that, like so:
for f in l1+l2+l3:
print[f]
Your use of the and
operator is incorrect. The other reason why your code doesn't work is using commas [like l1, l2, l3
] creates a tuple, which is a container that now holds your 3 lists. So when you try to loop through l1, l2, l3
it will loop through every element in that tuple [which are the lists] and not through every element in the lists as you intend.
answered Aug 20, 2012 at 15:55
LanaruLanaru
8,9127 gold badges33 silver badges62 bronze badges
If you want to print
1 4 7
2 5 8
3 6 9
Do:
for i,j,k in zip[l1,l2,l3]:
print i,j,k
answered Aug 20, 2012 at 15:56
inspectorG4dgetinspectorG4dget
106k25 gold badges137 silver badges235 bronze badges
It depends on what you want to achieve,
>>> #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
>>>
or the following implementation which will work in Python 2.7
>>> for f in itertools.chain[*itertools.izip[l1,l2,l3]]:
print f,
1 4 7 2 5 8 3 6 9
>>>
answered Aug 20, 2012 at 16:09
AbhijitAbhijit
60.1k18 gold badges127 silver badges197 bronze badges
It you're lists are not all the same length it is often better to use 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
answered Aug 20, 2012 at 16:06
Charles BeattieCharles Beattie
5,5571 gold badge29 silver badges32 bronze badges
1
To expand on top of Abhijit answer, you could use the itertools generator as the iterable within a list comprehension.
>>> [ n for n in itertools.chain[l1, l2, l3] ]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
answered Aug 20, 2012 at 17:39
DanDan
1,1492 gold badges10 silver badges18 bronze badges
If you mean that you have 3 lists of equal length and you want to print out their contents as 3 columns, then how about zip[] to connect the columns and a list comprehension to print[] on each iteration:
[ print[row] for row in zip[l1, l2, l3] ]
The above will print repr's of tuple[s]. If you want to format the values otherwise:
[ print["{} / {} / {}".format[*row]] for row in zip[l1, l2, l3] ]
Nobody said you have to use the output of a list comprehension.
answered Dec 20, 2017 at 19:28
pourhauspourhaus
5176 silver badges9 bronze badges