0

I was wondering how I could align every item in one list, to the corresponding index in the second list. Here is my code so far:

letters=['a','ab','abc','abcd','abcde','abcdef','abcdefg','abcdefgh','abcdefghi','abcdefghij'] numbers=[1,2,3,4,5,6,7,8,9,10] for x in range(len(letters)): print letters[x]+"----------",numbers[x] 

This is the output I get:

a---------- 1 ab---------- 2 abc---------- 3 abcd---------- 4 abcde---------- 5 abcdef---------- 6 abcdefg---------- 7 abcdefgh---------- 8 abcdefghi---------- 9 abcdefghij---------- 10 

This is the output I want:

a---------- 1 ab--------- 2 abc-------- 3 abcd------- 4 abcde------ 5 abcdef----- 6 abcdefg---- 7 abcdefgh--- 8 abcdefghi-- 9 abcdefghij- 10 
1
  • 1
    Try the tab character. Also prettytable. Good luck! Commented Apr 28, 2013 at 21:32

4 Answers 4

3

You could use string formatting:

for left, right in zip(letters, numbers): print '{0:-<12} {1}'.format(left, right) 

And the output:

a----------- 1 ab---------- 2 abc--------- 3 abcd-------- 4 abcde------- 5 abcdef------ 6 abcdefg----- 7 abcdefgh---- 8 abcdefghi--- 9 abcdefghij-- 10 
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best in terms of readability and sticks to KISS rule. The only thing to know is the max length of the first part - the rest is just replacing the '12' with it.
0

Something like this using string.formatting:

def solve(letters,numbers): it=iter(range( max(numbers) ,0,-1)) for x,y in zip(letters,numbers): print "{0}{1} {2}".format(x,"-"*next(it),y) ....: In [38]: solve(letters,numbers) a---------- 1 ab--------- 2 abc-------- 3 abcd------- 4 abcde------ 5 abcdef----- 6 abcdefg---- 7 abcdefgh--- 8 abcdefghi-- 9 abcdefghij- 10 

Comments

0
letters=['a','ab','abc','abcd','abcde','abcdef','abcdefg','abcdefgh','abcdefghi','abcdefghij'] for c,x in enumerate(letters, start=1): print x+("-"*(10-c))+" %s" % c a--------- 1 ab-------- 2 abc------- 3 abcd------ 4 abcde----- 5 abcdef---- 6 abcdefg--- 7 abcdefgh-- 8 abcdefghi- 9 abcdefghij 10 

Comments

0
letters=['a','ab','abc','abcd','abcde','abcdef','abcdefg','abcdefgh','abcdefghi','abcdefghij'] numbers=[1,2,3,4,5,6,7,8,9,10] for x in range(len(letters)): print '{0:11}{1}'.format(letters[x],numbers[x]).replace(' ','-'); a----------1 ab---------2 abc--------3 abcd-------4 abcde------5 abcdef-----6 abcdefg----7 abcdefgh---8 abcdefghi--9 abcdefghij-10 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.