Home > Blockchain >  how to get aligned output of two list?
how to get aligned output of two list?

Time:11-01

la=["ngsir","raise","kajggf","kajggsdda","kajgg","kajggkjabfkafku","kajgg","asakfaflg","as","sfowih","akjfglff"]

lb=["raise","kajggf","kajggkjabfkafku","cvsk","kajgg","asakfaflg","as","sfowih","akjfglff","kajggsdda","kajgg"]

print("team A:\t\t\t team B:") for i in range(11): print(la[i] "\t\t\t" lb[i])

i was expecting the two lists to be align, but got

team A: team B: rohit raise raise kajggf kajggf kajggkjabfkafku kajggsdda rohit kajgg kajgg kajggkjabfkafku asakfaflg kajgg as asakfaflg sfowih as akjfglff sfowih kajggsdda akjfglff kajgg

instead, how can i align them

CodePudding user response:

I believe that the below solution achieves what you are looking for.

la=["ngsir","raise","kajggf","kajggsdda","kajgg","kajggkjabfkafku","kajgg","asakfaflg","as","sfowih","akjfglff"]

lb=["raise","kajggf","kajggkjabfkafku","cvsk","kajgg","asakfaflg","as","sfowih","akjfglff","kajggsdda","kajgg"]


longest = max(map(len, la))

print("team A:"   (longest - 6) * " "   "\t\tteam B:") 
for i in range(11): 
    print(la[i]   (longest - len(la[i])) * " "   "\t\t\t"   lb[i])

CodePudding user response:

Use the just() Function to Print With Column Alignment in Python. Here we use ljust.

la=["ngsir","raise","kajggf","kajggsdda","kajgg","kajggkjabfkafku","kajgg","asakfaflg","as","sfowih","akjfglff"]

lb=["raise","kajggf","kajggkjabfkafku","cvsk","kajgg","asakfaflg","as","sfowih","akjfglff","kajggsdda","kajgg"]

print("team A:\t\t\t team B:") 
for i in range(11): 
    print(la[i].ljust(25) lb[i])

Output:

team A:                  team B:
ngsir                    raise
raise                    kajggf
kajggf                   kajggkjabfkafku
kajggsdda                cvsk
kajgg                    kajgg
kajggkjabfkafku          asakfaflg
kajgg                    as
asakfaflg                sfowih
as                       akjfglff
sfowih                   kajggsdda
akjfglff                 kajgg

CodePudding user response:

Take a look at How do I align text output in python? for more options. In your case, the following should work:

la=["ngsir","raise","kajggf","kajggsdda","kajgg","kajggkjabfkafkujkgflsk","kajgg","asakfaflg","as","sfowih","akjfglff"]

lb=["raise","kajggf","kajggkjabfkafkugfdfs","cvsk","kajgg","asakfaflg","as","sfowih","akjfglff","kajggsdda","kajgg"]

max_space = len(max((la),key=len)) 2 #adding 2 as the default space between the columns

str = "{0: <%d} {1}"%max_space
print(str.format("teamA:","teamB:")) 
for i in range(11): 
    print(str.format(la[i],lb[i]))

CodePudding user response:

To align two lists into separate columns, you can do the following:

team1 = ['Team A:', 'person1', 'person2', 'person3'] 
team2 = ['Team B:', 'person4', 'person5', 'person6']
string = ''

# Using the zip function, we can loop through the teams that correspond together.
for t1, t2 in zip(team1, team2): 
    string  = f'{t1}\t\t\t{t2}\n' # Append the string with a new row.

print(string)

Output:

Team A:                  Team B:
person1                  person4
person2                  person5
person3                  person6

  • Related