Loop print through two lists to get two columns with fixed(custom set) space between the first letter of each element of each list
Asked Answered
I

5

9

Suppose I have these two lists:

column1 = ["soft","pregnant","tall"]
column2 = ["skin","woman", "man"]

How do I loop print through these two lists while using a custom, fixed space(say 10, as in example) starting from the first letter of each element of the first list up to the first letter of each element of the second list?

Example output of a set spacing of 10:

soft      skin
pregnant  woman 
tall      man
Isosceles answered 23/8, 2012 at 12:32 Comment(1)
zip docs.python.org/library/functions.html#zipCarpic
B
9

Easily done with the string formatting,

column1 = ["soft","pregnant","tall"]
column2 = ["skin","woman", "man"]

for c1, c2 in zip(column1, column2):
    print "%-9s %s" % (c1, c2)

Or you can use str.ljust, which is tidier if you want to have the padding be based on a variable:

padding = 9
for c1, c2 in zip(column1, column2):
    print "%s %s" % (c1.ljust(padding), c2)

(note: padding is 9 instead of 10 because of the hard-coded space between the words)

Bluefield answered 23/8, 2012 at 12:41 Comment(4)
As written, both of these solutions will have three spaces, not two, between the "t" of "pregnant" and the "w" of "woman". You can remove the space in the printed string, I think.Campuzano
Suppose the elements of the second column would contain strings longer then the width of my terminal screen. The default behavior is that that those strings will 'spill' over my first column. How do let each such long strings continue from the starting point of the second column?Isosceles
@Campuzano True, reducing the padding-amount by one will match the expected output exactly. Removing the space between the %s might result in the words being smushed together. Edited answerBluefield
@Isosceles You could try clint which does column wrapping based on the terminal size. You could do something similar yourself with this answer and the textwrap module, but it would be rather fiddly..Bluefield
C
5

How about:

>>> column1 = ["soft","pregnant","tall"]
>>> column2 = ["skin","woman", "man"]
>>> for line in zip(column1, column2):
...     print '{:10}{}'.format(*line)
... 
soft      skin
pregnant  woman
tall      man
Campuzano answered 23/8, 2012 at 12:39 Comment(0)
G
3
column1 = ["soft","pregnant","tall"]
column2 = ["skin","woman", "man"]

for row in zip(column1, column2):
    print "%-9s %s" % row # formatted to a width of 9 with one extra space after
Grogshop answered 23/8, 2012 at 12:36 Comment(0)
B
1

Using Python 3

column1 = ["soft","pregnant","tall"]
column2 = ["skin","woman", "man"]

for line in zip(column1, column2):
    print('{:10}{}'.format(*line))
Burly answered 29/9, 2015 at 9:54 Comment(0)
P
0

One liner using new style string formatting:

>>> column1 = ["soft", "pregnant", "tall"]
>>> column2 = ["skin", "woman", "man"]

>>> print "\n".join("{0}\t{1}".format(a, b) for a, b in zip(column1, column2))

soft        skin
pregnant    woman
tall        man
Papaya answered 29/7, 2014 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.