Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing all the values from multiple lists at the same time

Tags:

python

list

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.

like image 999
cyberbemon Avatar asked Aug 20 '12 15:08

cyberbemon


People also ask

How do you print multiple lists in Python?

Use * Operator to Print a Python List. Another way to print all of the contents of a list is to use the * or "splat" operator. The splat operator can be used to pass all of the contents of a list to a function.

How do I print multiple variables when printing?

To print multiple variables in Python, use the print() function. The print(*objects) is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.

How do I print a list of elements in the same line?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.


1 Answers

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 ]
like image 57
mgilson Avatar answered Oct 26 '22 23:10

mgilson