Printing 2 evenly populated lists side by side evenly
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
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))