1

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

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

2 Comments

Can we put this in a csv, while keeping the tabular format?
Well, if it's just numbers then it's enough to print a comma between adjacent numbers. If you have to put strings (thus they can contain commas) you should look the csv module up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.