Palaios
Palaios

Reputation: 57

print a nested list so that each list within the list is in a different column

I have the following code

F_list= []
C_list= []
C_approx_list = []
for F in range(0,101,10):
    F_list.append(F)
    C_approx_list.append((F-30)/2.)
    C_list.append((F-32)*(5./9))

conversion = [F_list,C_approx_list,C_list]

for list in conversion:
    for number in list:
        print number

With the following output:

"""Test Execution
Terminal > python exercise2_18.py
0
10
20
30
40
50
60
70
80
90
100
-15.0
-10.0
-5.0
0.0
5.0
10.0
15.0
20.0
25.0
30.0
35.0
-17.7777777778
-12.2222222222
-6.66666666667
-1.11111111111
4.44444444444
10.0
15.5555555556
21.1111111111
26.6666666667
32.2222222222
37.7777777778
"""

I want the three lists to be on the following form instead:

F_list C_approx C_list

Instead of:

  1. F_list
  2. C_approx
  3. C_list

Thanks for any potential answer!

Upvotes: 1

Views: 1342

Answers (2)

Andy Hayden
Andy Hayden

Reputation: 375885

You could write this using a list comprehension:

conversion = [(F, (F-30)/2., (F-32)*(5./9)) for F in range(0,101,10)]

for line in conversion:
    print temp[0], temp[1], temp[2]
    # print(*temp)                           # in python3

You can move to your previous lists using zip:

F_list, C_approx_list, C_list = zip(*conversion)

Upvotes: 1

Michael Mauderer
Michael Mauderer

Reputation: 3882

for a,b,c in zip(F_list, C_approx_list, C_list)
    print(a,b,c)

Or for output that might be easier to read

for a,b,c in zip(F_list, C_approx_list, C_list)
    print('{}\t{}\t{}'.format(a,b,c))

or simpler in Python 3.X

for a,b,c in zip(F_list, C_approx_list, C_list)
    print(a,b,c, sep='\t')

Upvotes: 3

Related Questions