10

I'm using the following code to produce 2 lists, nameList and gradeList.

nameList[]        
gradeList[]
for row in soup.find_all('tr'):
        name = row.select('th strong')
        grade = row.select('td label')
        if grade and name:
            if "/" in grade[0].text:
                gradeList.append(grade[0].text)
                nameShort = re.sub(r'^(.{20}).*$', '\g<1>...', str(name[0].text))
                nameList.append(nameShort)

Producing something like:

nameList = [“grade 1”,”grade 2222222222”,”grade 3”]
gradeList = [“1/1”,”2/2”,”100000/100000”]

I want the program to print the lists in 2 clean columns, side by side. Within each column, I want the data to align to the left. The lists (without fail) will always be evenly populated. The first column (nameList) will never be longer than 25 characters. What I am looking for would be similar to the following:

        Assignment          Grade
0       grade 1             1/1
1       grade 2222222222    2/2
2       grade 3             100000/100000

I've tried to use pandas and it worked, but the formatting was weird and out of place. It wouldn't align to the left like I want. I believe this happened because the data each has a different character length in both lists (shown above).

falsetru
  • 357,413
  • 63
  • 732
  • 636
Jackson Blankenship
  • 475
  • 2
  • 7
  • 19

3 Answers3

16

Using str.format:

nameList = ["grade 1", "grade 2222222222", "grade 3"]
gradeList = ["1/1", "2/2", "100000/100000"]

fmt = '{:<8}{:<20}{}'

print(fmt.format('', 'Assignment', 'Grade'))
for i, (name, grade) in enumerate(zip(nameList, gradeList)):
    print(fmt.format(i, name, grade))

output:

        Assignment          Grade
0       grade 1             1/1
1       grade 2222222222    2/2
2       grade 3             100000/100000

Alternatively, you can also use printf style formatting using % operator:

fmt = '%-8s%-20s%s'

print(fmt % ('', 'Assignment', 'Grade'))
for i, (name, grade) in enumerate(zip(nameList, gradeList)):
    print(fmt % (i, name, grade))
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    What is "fmt = '{:<8}{:<20}{}'" defining? When I run this code, the program prints out the list like 20 times. – Jackson Blankenship Dec 27 '14 at 03:05
  • @JacksonBlankenship, It's a format string to instruct `str.format` how to format arguments: `{:<8}` is shrotcut for `{0:<8}`. `0` mean 0th (first argument), `:` is to begin format specification, `<`: left alignment, `8` means width. – falsetru Dec 27 '14 at 03:07
  • @JacksonBlankenship, If you want to know more about Format string syntax, following the link in the answer or this: [Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings) – falsetru Dec 27 '14 at 03:08
  • @JacksonBlankenship, For `printf`-style formatting, follow this link: [`printf`-style String Formatting](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) – falsetru Dec 27 '14 at 03:09
  • Oh it got caught up in another for loop! My bad, works great! Thanks! – Jackson Blankenship Dec 27 '14 at 03:10
8

Given two lists

nameList = ['grade 1','grade 2222222222','grade 3']
gradeList = ['1/1','2/2','100000/100000']

tab separated format. using zip() two iterate through both lists at the same time

print 'Assignment \t\tGrade' 
for n,g in zip(nameList,gradeList):
    print n + '\t\t\t' + g


Assignment         Grade
grade 1            1/1
grade 2222222222   2/2
grade 3            100000/100000
Bob Haffner
  • 8,235
  • 1
  • 36
  • 43
0

You can use F-Strings, I think it becomes more clear:

nameList = ["grade 1", "grade 2222222222", "grade 3"]
gradeList = ["1/1", "2/2", "100000/100000"]

# Table header
print(f'{"":<8} {"Assignment":<20} {"Grade":<20}')

# Table content
for i, (name, grade) in enumerate(zip(nameList, gradeList)):
    print(f'{i:<8} {name:<20} {grade:<20}')

Result:

         Assignment           Grade               
0        grade 1              1/1                 
1        grade 2222222222     2/2                 
2        grade 3              100000/100000
Alexandre Paes
  • 121
  • 2
  • 4