0

I'm getting a list such as

StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']

I am printing it like:

for each in StudentStatus:
    print each

Hence the output is

Physics Pass
Chem Distinction
Maths Average

I want to print it the other way. I tried -

for each in StudentStatus:
    print each[0], each[1]

which just printed

Physics
Chem
Maths

How to split the list to get proper output?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
NikAsawadekar
  • 57
  • 1
  • 5

2 Answers2

0

Assuming the last word is the grade, then you can use str.rpartition:

StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']

for item in StudentStatus:
    print 'Subject: {0} - Grade: {2}'.format(*item.rpartition(' '))

Output:

Subject: Physics - Grade: Pass
Subject: Chem - Grade: Distinction
Subject: Maths - Grade: Average
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0
StudentStatus = ['Physics Pass', 'Chem Distinction', 'Maths Average']
for echo in StudentStatus:
    first, second = echo.split()
    print first, second
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129