kevin
kevin

Reputation: 2014

Python: generating list outputs as a table

I have the data in two lists.

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]

So I use a nested for loop:

for a in list1:
    for b in list2:
        distance = a - b
        print distance

This returns:

-5
-6
-7
..
..
..
-4
-5

I would like to have the output as tabular format:

 -5 -6 -7 -8 -9
 -4 -5 -6 -7 -8
 ...
 ...
 -1 -2 -3 -4 -5

Upvotes: 1

Views: 29

Answers (1)

William
William

Reputation: 2935

for a in list1:
    for b in list2:
        distance = a - b
        print distance,
    print

The output I get:

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

Upvotes: 2

Related Questions